简体   繁体   English

如何通过嵌套在对象数组的属性中的对象属性的值在对象数组中找到多个索引?

[英]How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects?

I have an array of objects, each object has a property of "watched" that is an array of objects by itself.我有一个对象数组,每个对象都有一个“监视”属性,它本身就是一个对象数组。

let arr = 
        [ 
            {   
                id: 1, 
                name: "A", 
                watched: [ 
                            {movieId: 7, movieName: "Avatar"} , 
                            {movieId: 8, movieName: "Titanic"}
                        ]
            },
            {   
                id: 2, 
                name: "B", 
                watched: [   
                            {movieId: 1, movieName: "Armageddon"} , 
                            {movieId: 8, movieName: "Titanic"}
                        ]
            },
            {   
                id: 3, 
                name: "C", 
                watched: [   
                            {movieId: 1, movieName: "Armageddon"} , 
                            {movieId: 7, movieName: "Avatar"}
                        ]
            }

        ]

I need an array of indexes according to a given MovieId.我需要根据给定的 MovieId 的索引数组。 If I want an array of indexes who watched movieId no.如果我想要一个观看电影的索引数组,没有。 7 (Avatar) - I'll get: 7(阿凡达) - 我会得到:

let AvatarWatchers = [0,2]让 AvatarWatchers = [0,2]

I tried:我试过:

AvatarWatchers = arr.filter((item) => item.watched.find(movieObj => movieObj.movieId == 7) > -1 )

But It's not working and I'm getting an empty array.但它不起作用,我得到了一个空数组。

 let arr = [ { id: 1, name: "A", watched: [ {movieId: 7, movieName: "Avatar"} , {movieId: 8, movieName: "Titanic"} ] }, { id: 2, name: "B", watched: [ {movieId: 1, movieName: "Armageddon"} , {movieId: 8, movieName: "Titanic"} ] }, { id: 3, name: "C", watched: [ {movieId: 1, movieName: "Armageddon"} , {movieId: 7, movieName: "Avatar"} ] } ]; let result = arr.map((item, index) => ({...item, index})) .filter(item => item.watched.find(watchedItem => watchedItem.movieName === 'Avatar')) .map(item => item.index); console.log('expected result', result);

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

相关问题 在具有多个属性名称和值的嵌套对象数组中查找对象数组 - find in array of objects with nested array of objects with multiple property name and value 如何在对象数组中查找和编辑对象的属性? - How to find and edit property of object in the array of an objects? 如何比较数组的多个对象中的属性值? - How to compare a property value in multiple objects of an array? 使用属性值从对象数组中查找并返回 object - Find and Return an object from an array of array of objects using a property value 将对象中的数组属性扩展为使用数组值填充的多个对象 - Expand Array property in Object to Multiple Objects populated w/ Array value 如何将对象的属性与对象数组的属性匹配? - How to match a property of an object to a property of an array of objects? 在对象数组中查找具有 id 属性最大值的对象 - Find object having maximum value for the `id` property in an array of objects 在嵌套的对象数组中按属性路径查找对象 - Find object by property path in nested array of objects 通过属性在对象数组中查找值的语法是什么? - What is the syntax to find a value in an array of objects by a property? AngularJS使用对象属性对多个对象数组进行排序 - AngularJS order multiple objects array with object property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM