-1

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.

0

2 Answers 2

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

or, if you use underscore and es5 only

_.map(mainarray, function(item){
    return item.innerarray[1].propertyiwant
});
Sign up to request clarification or add additional context in comments.

2 Comments

Well, Array.prototype.map is in ES5 :-)
yep:) i just presented approach with arrow functions for example:)
2

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);

5 Comments

Hey thanks for your time, i tried what you said: mapped = mainarray.map(e => e.innerarray[2].propertyiwant) but it throws me an error of "cant read property of undefined" . i also tried mainarray.map(e => e.innerarray[2]['propertyiwant']) but still doesnt work
Arrays start at index 0, so you want innerarray[1].
@krimz repl.it/C0aO basically this is exactly what Foker had suggested.
krimz, both this and @Foker's answer worked fine when I tested them. You should give your expected output, and show what you've tried so far.
@FizzyTea hey! you're right, they work just fine with these examples. I was looking for something else, i was just not clear enough

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.