首页 > 代码库 > js笔记

js笔记

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);    }

 

js笔记