简体   繁体   English

从另一个对象数组过滤字符串数组中的值

[英]filter values from an array of strings from another array of objects

I am trying to get only those strings which are not present in array of objects我试图只获取那些不存在于对象数组中的字符串

var objects = [{name:'a',is:false},{name:'b',is:false},{name:'c',is:false}];
var strings = ['a','b','z','x'];

let result = strings.filter(o1 => !objects.includes(o2=> o2.name === o1));
console.log(result)

so the result should be only 'z' and 'x'所以结果应该只是'z'和'x'

From your objects array, you can create Map ( lookup ) which has keys as the name property and values as the negated version of the is property.从你的objects数组,你可以创建Map ( lookup ),它的键是name属性,值is属性的否定版本。

Map {
  'a': true,
  'b': true,
  'c': true
}

You can then use this to lookup to filter your array of strings.然后,您可以使用它来查找过滤字符串数组。 If the Map has a value which is true then you can remove it from your array (by returning false), otherwise, if the Map doesn't have the current string, you can keep it in the array.如果 Map 的值为true则您可以将其从数组中删除(通过返回 false),否则,如果 Map 没有当前字符串,则可以将其保留在数组中。 Creating a map/object by preprocessing your objects array saves you from needing to use an inner loop inside of your filter() , improve the overall efficiency of your code if your array sizes increase.通过预处理对象数组来创建地图/对象可以使您无需在filter()内部使用内部循环,如果数组大小增加,则可以提高代码的整体效率。

 const objects = [{name:'a',is:false},{name:'b',is:false},{name:'c',is:false}]; const strings = ['a','b','z','x']; const lookup = new Map(objects.map(({name, is}) => [name, !is])); const result = strings.filter(v => !lookup.get(v)); console.log(result);


Your approach was very close.你的方法非常接近。 However, the .includes() method doesn't accept a callback function like you're providing it with ( .includes() will search your array for that same function object references your providing it with)但是, .includes()方法不接受像您提供的回调函数( .includes()将在您的数组中搜索您提供的相同函数对象引用)

 const cb = v => v; // fake callback const arr = [cb]; // some array contains that callback console.log(arr.includes(cb)); // providing the callback as a function to includes // => true, as the `cb` function is inside of `arr`

If you want to provide your own "includes" logic, you can use .some() instead:如果你想提供你自己的“包含”逻辑,你可以使用.some()代替:

 var objects = [{name:'a',is:false},{name:'b',is:false},{name:'c',is:false}]; var strings = ['a','b','z','x']; let result = strings.filter(o1 => !objects.some(o2 => o2.name === o1)); console.log(result)

If you're curious, the above logic with .some() has a time complexity of O(N*M), whereas the Map approach has a time complexity of O(N+M), where N is the size of your strings array and M is the size of your objects array.如果您好奇,上面的.some()逻辑的时间复杂度为 O(N*M),而 Map 方法的时间复杂度为 O(N+M),其中 N 是字符串的大小array 和 M 是对象数组的大小。

Used map to simplify the array and then used filter and includes to find out the strings which are not part of objects .使用map来简化数组,然后使用 filter 和 includes 来找出不属于objectsstrings

 var objects = [{name:'a',is:false},{name:'b',is:false},{name:'c',is:false}]; var strings = ['a','b','z','x']; let names = objects.map(dataItem => dataItem.name) let filteredNames = strings.filter(stringItem => !names.includes(stringItem)) console.log(filteredNames)

includes now allow callback function.现在包括允许回调函数。

instead of this use find:而不是这个使用查找:

 var objects = [{name:'a',is:false},{name:'b',is:false},{name:'c',is:false}]; var strings = ['a','b','z','x']; let result = strings.filter(o1 => !objects.find(o2=> o2.name === o1)); console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM