TheDevGuy
TheDevGuy

Reputation: 703

return a certain key:value of object

I have a array of object like this

AnArray: [
     {   name: 'name1',
         id: 123456,
         arrayOfSomething: [[1], [2], [3]]
     },
     {   name: 'name2',
         id: 123456,
         arrayOfSomething: [[0], [2], [1]]
     }

I need to push just the arrayOfSomething array in the result array, so I do:

SaveMyResult(){
   this.result.push({
    something:this.AnArray})
}

but it pushing me all the object data, How can I do ?

Upvotes: 0

Views: 48

Answers (1)

IceMetalPunk
IceMetalPunk

Reputation: 5556

If you push AnArray, then yes, the result will be that AnArray is added to the end of your results array. If you don't want that, and you only want one property from each object, use the map method and concatenate the final array it creates:

this.result = this.result.concat(this.AnArray.map(({arrayOfSomething}) => ({arrayOfSomething})));

Here I've used a bit of destructuring to shorten the code, but it's basically going through every element of the array, pulling out its arrayOfSomething property, and replacing the element with a new object that contains only that property.

Upvotes: 1

Related Questions