简体   繁体   中英

Find object by property in an array of JavaScript objects inside another array

I have an array called mainarray which has three objects inside it. Inside each object there's another array called innerarray which has its own three objects.

How do I get the first property of every second object inside each of the innerarray ? Is this possible?

mainarray: [{
    innerarray: [{
        property1: value1,
        property2: value2,
        property3: value3,
    }, {
        propertyiwant: value1,
        property2: value2,
        property3: value3,
    }, {
        property1: value1,
        property2: value2,
        property3: value3,
    }]
}, {
    innerarray: [{
        property1: value1,
        property2: value2,
        property3: value3,
    }, {
        propertyiwant: value1,
        property2: value2,
        property3: value3,
    }, {
        property1: value1,
        property2: value2,
        property3: value3,
    }]
}, { 
    innerarray: [{
        property1: value1,
        property2: value2,
        property3: value3,
    }, {
        propertyiwant: value1,
        property2: value2,
        property3: value3,
    }, {
        property1: value1,
        property2: value2,
        property3: value3,
    }]
}]

This question is totally different than From an array of objects, extract value of a property as array . As here i am trying to get property of an object inside an array which is inside another array.

mainarray.map(item => item.innerarray[1].propertyiwant);

or, if you use underscore and es5 only

_.map(mainarray, function(item){
    return item.innerarray[1].propertyiwant
});

You can try the following code. It will bring you every second (in the sense of 2nd, 4th, 6th... etc) object in each subarray if they exist. If you are interested in only the second object then it's a matter of replacing the whole e.innerarray.reduce(.. . part with e.innerarray[2] . This will get you the whole desired 2nd object and then you can access any property of it that you like.

 var mainarray = [{ innerarray: [{ property1: "value1", property2: "value2", property3: "value3", }, { propertyiwant: "value1", property2: "value2", property3: "value3", }, { property1: "value1", property2: "value2", property3: "value3", }] }, { innerarray: [{ property1: "value1", property2: "value2", property3: "value3", }, { propertyiwant: "value1", property2: "value2", property3: "value3", }, { property1: "value1", property2: "value2", property3: "value3", }] }, { innerarray: [{ property1: "value1", property2: "value2", property3: "value3", }, { propertyiwant: "value1", property2: "value2", property3: "value3", }, { property1: "value1", property2: "value2", property3: "value3", }] }], mapped = mainarray.map(e => e.innerarray.reduce((p,c,i) => (i%2 && p.push(c),p),[])); console.log(mapped); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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