简体   繁体   English

根据第二个数组中的值过滤对象数组

[英]Filter array of objects based on values in second array

I have an array of objects that I'd like to filter to create a new array based on whether or not the value of any key matches any value in another array. 我有一个对象数组,我想根据任何键的值是否匹配另一个数组中的任何值来过滤以创建一个新数组。

const array1 = [{name: 'pink', id: 13}, {name: 'orange', id: 17}, {name: 'red, id: 64}, {name: 'purple', id: 47}, {name: 'yellow', id: 23}, {name: 'gray', id: 2}, {name: 'black', id: 200}, {name: 'violet', id: 4}]

const array2 = ['red', 'blue', 'green', 'pink']

I've tried using a for...of loop inside of a return function but that is giving me errors. 我试过在返回函数中使用for ... of循环,但这给了我错误。

    const array3 = array1.filter(color => {
        for (mainColor of array2){
            return color.name === mainColor 
        }
    });

This works but is clearly not the best way. 这有效,但显然不是最佳方法。

    const array3 = array1.filter(color => {
            return (color.main === 'red') || (color.main === 'blue')
        });

How can I get a third array from array1 that contains only the objects where the array1.name matches a value in array2? 如何从array1获得第三个数组,该数组仅包含array1.name与array2中的值匹配的对象?

Is it possible with ES6 or Lodash? ES6或Lodash是否有可能?

Thanks in advance! 提前致谢!

Almost there, instead of for-loop use includes and return 几乎在那里,而不是for循环使用includesreturn

const array3 = array1.filter(color => {
    return array2.includes ( color.name );
});

Or 要么

const array3 = array1.filter( color => array2.includes ( color.name ) );

Let me give an alternative, that has slightly more code, but is more efficient as well, as it only needs to scan array2 once: 让我给出一个替代方案,它具有稍微更多的代码,但是效率也更高,因为它只需要扫描一次array2

 const array1 = [{name: 'pink', id: 13}, {name: 'orange', id: 17}, {name: 'red', id: 64}, {name: 'purple', id: 47}, {name: 'yellow', id: 23}, {name: 'gray', id: 2}, {name: 'black', id: 200}, {name: 'violet', id: 4}], array2 = ['red', 'blue', 'green', 'pink']; const colorSet = new Set(array2), array3 = array1.filter(color => colorSet.has(color.name)); console.log(array3); 

Try the following with Array's includes() : 使用Array的includes()尝试以下操作:

 const array1 = [{name: 'pink', id: 13}, {name: 'orange', id: 17}, {name: 'red', id: 64}, {name: 'purple', id: 47}, {name: 'yellow', id: 23}, {name: 'gray', id: 2}, {name: 'black', id: 200}, {name: 'violet', id: 4}] const array2 = ['red', 'blue', 'green', 'pink']; const array3 = array1.filter(color => array2.includes(color.name)); console.log(array3); 

The filter method already iterates on each item. filter方法已经在每个项目上进行迭代。 You just have to return true if the element is present in the second array (by using indexOf or includes ) 如果该元素存在于第二个数组中,则只需return true (通过使用indexOfincludes

 const array1 = [{name: 'pink', id: 13}, {name: 'orange', id: 17}, {name: 'red', id: 64}, {name: 'purple', id: 47}, {name: 'yellow', id: 23}, {name: 'gray', id: 2}, {name: 'black', id: 200}, {name: 'violet', id: 4}] const array2 = ['red', 'blue', 'green', 'pink']; let filteredArray = array1.filter(e => array2.indexOf(e.name) > -1); console.log(filteredArray); 

I know indexOf might seem a little outfashioned and ES5-ish, but it still does the job: 我知道indexOf可能看起来有些过时且具有ES5风格,但仍然可以完成工作:

 const array1 = [{name: 'pink', id: 13}, {name: 'orange', id: 17}, {name: 'red', id: 64}, {name: 'purple', id: 47}, {name: 'yellow', id: 23}, {name: 'gray', id: 2}, {name: 'black', id: 200}, {name: 'violet', id: 4}], array2 = ['red', 'blue', 'green', 'pink']; let array3 = array1.filter(x => array2.indexOf(x.name) != -1) console.log(array3) 

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

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