标签:对象 min 克隆 return shuffle length nbsp ring copy
1.克隆对象
克隆数组:
var country=[‘中国‘,‘美国‘]; var copyCountry=country.slice(0);
克隆对象:
var people={sex:‘man‘,age:4}; var me=JSON.parse(JSON.stringify(people));
2.随机数
从数组中随机取n个不重复的元素
function getRandomArrayElements(arr, count) { var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index; while (i-- > min) { index = Math.floor((i + 1) * Math.random()); temp = shuffled[index]; shuffled[index] = shuffled[i]; shuffled[i] = temp; } return shuffled.slice(min); }
标签:对象 min 克隆 return shuffle length nbsp ring copy
原文地址:http://www.cnblogs.com/CallmeYhz/p/6170815.html