Last active
August 29, 2016 07:35
-
-
Save linkgod/9f08e4764eef85c04daa to your computer and use it in GitHub Desktop.
随机截取一个列表中的元素,返回列表
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @description 获取一个list孩子随机生成器 | |
* @param {Array} list 列表 | |
* @param {Array} shown 已经显示过的项目 | |
* @return {Function} list随机孩子的列表生成器,执行之后返回一个随机孩子列表 | |
* */ | |
function randomChildGenerator(list) { | |
var listProduct = list, | |
listCustom = []; | |
if (!S.isArray(list)) { | |
S.error('first parma should be array'); | |
return ; | |
} | |
return function(amount){ | |
var r = [], | |
temp, | |
item; | |
amount = amount ? amount : 1; | |
if (listProduct.length < amount) { | |
r = r.concat(listProduct); | |
temp = listProduct; | |
listProduct = listCustom; | |
listCustom = temp; | |
} | |
for (var len = listProduct.length; r.length < amount; len--) { | |
item = listProduct.splice(util.randomInt(len - 1), 1)[0]; | |
r.push(item); | |
listCustom.push(item); | |
} | |
return r; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment