0

I have an array of items with this structure:

originalArray = [
  {
    product: { price: 10},
    shipping: {...}
  },
  {
    product: {price: 20},
    shipping: {...},
  }
]

I want to make a new array that is just the products from each original array item, like:

[ {price: 10}, {price: 20} ]

Using javascript (es6/2015 is fine)

What's the fastest way to do this? Is there any way to do it without a loop / map? The amount of items in the array will be dynamic / I will not know how many there might be.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
mheavers
  • 29,530
  • 58
  • 194
  • 315

1 Answers1

1

The easiest would be to just map it

var originalArray = [{
    product: {
      price: 10
    },
    shipping: {}
  },
  {
    product: {
      price: 20
    },
    shipping: {},
  }
]

var newArray = originalArray.map(item => item.product);

console.log( newArray )

There's really no way to do it without iterating

adeneo
  • 312,895
  • 29
  • 395
  • 388