我误解了如何删除具有与另一个数组匹配的元素的数组中的元素。
例:
const items = ['first', 'second', 'third']
const secondItems = ['first', 'second']
预期:
console.log(items) | 'third'
console.log(secondItems) | 'first', 'second
希望有一些例子可以实现。多次尝试使用两个forEach或filter和checking语句,但是我总是得到错误的结果。
这可以通过以下方法解决:
const items = ['first', 'second', 'third'];
const secondItems = ['first', 'second'];
const filteredBySecondItems = items.filter(function(item) {
// For item in items array, check if there is a match
// in secondItems. If there is no match, allow it
// to be included in the returned list
return secondItems.indexOf(item) === -1
})
console.log(filteredBySecondItems) // [ 'third' ]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句