gpt4 book ai didi

javascript - 将数组的项映射到另一个数组

转载 作者:行者123 更新时间:2023-11-28 19:51:50 25 4
gpt4 key购买 nike

我有一个数组:

arr = [ 1, 2 , 3 ]

还有另一个数组,我将 DOM 元素保存为

Elem = [ 1, 3]

我需要迭代 arr 并且仅在索引匹配时才执行操作。例如,因为我有 elem 1 和 3,所以当我循环 arr 时,应该只发生 1 和 3 的事情,并且应该跳过 2,因为没有 elem 2。

有人告诉我研究关联数组,我想知道如何用最少的行数来做到这一点。

我希望代码简单易读,到目前为止,所有关联数组的示例都毫无意义并且臃肿。

最佳答案

for(var i = 0;i<arr.length;i++){
if(Elem.indexOf(arr[i])>-1){
//Elem contains arr[i] (contains object that at index i in arr)
//will be called only for 1 and 3 in arr
arr[i] = ... //do what you want with this object.
}
}

你是这个意思吗?

关于javascript - 将数组的项映射到另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23311091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com