var arr = [2, 6, 4, 4, 4, 9]var newArr = (arr.sort())for (var i = 0; i < newArr.length; i++) if (newArr[i] == newArr[i + 1]) { var rem = [console.log(i)] //- (a) }console.log(rem[0])i我想要数组中数字重复的所有值newArr。语句 (a) 给出了索引,我想将它们存储在名为 的数组中rem。然而console.log(rem[0])是未定义的。我能做些什么?
2 回答
千巷猫影
TA贡献1829条经验 获得超7个赞
你是这个意思?
var arr = [2, 6, 4, 4, 4, 9]
var newArr = (arr.sort())
var rem = []; // create your rem array outside
for (var i = 0; i < newArr.length; i++) {
if (newArr[i] == newArr[i + 1]) {
rem.push(i); // if "i" matches your criteria, add it to the array.
}
}
console.log(rem); // rem has the full list of indices
qq_笑_17
TA贡献1818条经验 获得超7个赞
也许是这样的?
var arr = [2, 6, 4, 4, 4, 9]
var newArr = (arr.sort())
vare rem = []
for (var i = 0; i < newArr.length; i++)
if (newArr[i] == newArr[i + 1]) {
rem.push(newArr[i]);
}
console.log(rem[0])
添加回答
举报
0/150
提交
取消