gpt4 book ai didi

javascript - 当在 JavaScript 中的另一个数组中获取匹配项时,从数组中删除元素

转载 作者:行者123 更新时间:2023-12-03 06:43:55 25 4
gpt4 key购买 nike

我在 javascript 中有 2 个数组

数组 1

var columns=[{name: 'id', default: true}, {name: 'type', default: true},{name: 'typeName', default: true}, {name: 'client', default: false}];

数组2

var unSelect=["id", "type", "typeName"]

现在我想要一个新数组,即Array 3,其中只有不匹配的元素匹配即名称

在这种情况下

var array 3=[{name: 'client', default: false}]

我尝试过拼接,但我的索引不匹配。

最佳答案

这是使用Array.prototype.map函数执行此操作的最简单方法。该函数可以迭代一个数组并在每次迭代时返回您想要的内容。

var array3 = columns.map(function(item) {

if (unselected.indexOf(item.name) < 0) //see if the name of an element object is in the unselected array
return item; // if yes, return the element
})

关于javascript - 当在 JavaScript 中的另一个数组中获取匹配项时,从数组中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37832338/

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