我是否可以访问数组中的所有其他项?所以基本上,位置 0、2、4、6 等中的所有项目。
如果有帮助,这是我的代码:
function pushToHash(key, value) {
for (var t = 0; t < value.length; t++) {
MQHash[key[t]] = value.slice(0, lineLength[t]);
}
}
因此,我需要获取 lineLength
的所有其他值。我只想要 lineLength
,而不是 key
。我正在考虑做一个模数,但不确定我将如何实现它。有什么想法吗?
提前致谢!
最佳答案
您可以像这样在数组过滤方法中使用索引(第二个参数):
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// filter out all elements that are located at an even index in the array.
let x = arr.filter((element, index) => {
return index % 2 === 0;
})
console.log(x)
// [1, 3, 5, 7, 9]
关于javascript - 访问数组中的所有其他项 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30713250/