我得到了这段代码:
var counter = 0,
randoms = [],
randoms1 = [],
n;
for (n = 0; n < 5; n++) {
randoms.push(Math.floor(Math.random() * 49 + 1));
randoms1.push(Math.floor(Math.random() * 49 + 1));
}
使用这2个数组,如何检查它们中是否有一个公共数字,然后将此数字将其添加到新数组中?
循环遍历其中一个数组并检查:
var matches = [];
for (var i = 0; i < randoms.length; i++) {
if (randoms1.indexOf(randoms[i]) > -1) matches.push(randoms[i]);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句