-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.

Community
  • 1
  • 1
krimz
  • 87
  • 10

2 Answers2

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
});
1983
  • 5,882
  • 2
  • 27
  • 39
Foker
  • 944
  • 2
  • 9
  • 22
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);
Redu
  • 25,060
  • 6
  • 56
  • 76
  • 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 – krimz Jun 10 '16 at 15:01
  • 1
    Arrays start at index 0, so you want `innerarray[1]`. – 1983 Jun 10 '16 at 15:03
  • @krimz https://www.repl.it/C0aO basically this is exactly what Foker had suggested. – Redu Jun 10 '16 at 16:13
  • 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. – 1983 Jun 10 '16 at 18:22
  • @FizzyTea hey! you're right, they work just fine with these examples. I was looking for something else, i was just not clear enough – krimz Jun 10 '16 at 19:48