javascript
js常用的工具函数整理
Dreamer 发布于 2016-02-11 收录于 前端开发
随机从数组中取出count个元素 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /** * @param {[type]} 数组 * @param {[type]} 取出元素的个数 * @return {[type]} [description] */ 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); } 数组去重 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 //数组去重复 Array.prototype.distinct = function() { var arr = this, result = [], i, j, len