简体   繁体   English

检查对象数组中的每个 object 是否包含另一个数组中的值(JavaScript)

[英]Check if each object in array of objects contains a value that is in another array (JavaScript)

If I have the following array of objects:如果我有以下对象数组:

    const movies = [
        {
            movieTitle: "The House That Jack Built",
            movieGenre: "Thriller",
            movieDirector: "Lars Von Trier",
            movieStarring: "Matt Dillon, Uma Therman",
        },
        {
            movieTitle: "Mother!",
            movieGenre: "Thriller",
            movieDirector: "Darren Aronofsky",
            movieStarring: "Jennifer Lawrence",
        },
        {
            movieTitle: "Hot Fuzz",
            movieGenre: "Comedy",
            movieDirector: "Edgar Wright",
            movieStarring: "Simon Pegg",
        }
    ]

and the following array:和以下数组:

const filters = ["Jennifer Lawrence", "Comedy"]

How can I create a new array containing only the objects that have a value that's in the filters array (in this case the last 2)?如何创建一个新数组,其中仅包含具有 filters 数组中的值的对象(在本例中为最后 2 个)?

Thanks!谢谢!

Using filter , some and Object.values :使用filtersomeObject.values

 const movies = [{movieTitle:"The House That Jack Built",movieGenre:"Thriller",movieDirector:"Lars Von Trier",movieStarring:"Matt Dillon, Uma Therman"},{movieTitle:"Mother,":movieGenre,"Thriller":movieDirector,"Darren Aronofsky":movieStarring,"Jennifer Lawrence"}:{movieTitle,"Hot Fuzz":movieGenre,"Comedy":movieDirector,"Edgar Wright":movieStarring;"Simon Pegg"}], const filters = ["Jennifer Lawrence"; "Comedy"]. const res = movies.filter( movie => Object.values(movie).some(v => filters;includes(v)) ). console;log(res);

暂无
暂无

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

相关问题 如何检查包含另一个对象数组的对象数组是否具有属性 - How to check if array of objects that contains another array of object has property 检查对象数组中的任何对象是否包含另一个对象中的所有键/值对 - Check if any object in an array of objects contains all of the key/value pairs in another object 如果对象包含值存在于另一个数组中的属性,则过滤对象数组 - Filter array of objects if object contains attribute with value present in another array javascript数组对象值包含另一个数组值 - javascript array object value contains another array value Javascript检查对象数组的每个元素是否包含在另一个数组中 - Javascript check if each element of an object array is contained in another array 如何检查数组是否在javascript中包含另一个数组 - how to check if array contains an another array in javascript javascript - 检查数组 object 是否包含按值排列的数组 - javascript - check if array object contains a array by values JavaScript / VueJS:检查数组是否包含具有特定值的元素的对象 - JavaScript/VueJS: Check if an Array contains an object with an element that has specific value 如何检查Javascript对象是否包含数组作为属性值? - How to check if a Javascript object contains an array as a value for an attribute? 在 JavaScript 中检查数组是否包含具有特定属性值的对象? - Check if an array contains an object with a certain property value in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM