-1

I have a array like this:

let arr = [
   {
     index: 1,
     price: "24.99"
   },
   {
     index: 2,
     price: "24.95"
   },
   {
     index: 3,
     price: "20.95"
   },
]

I want only the prices now in the array like this:

let arr = ["24.99", "24.95", "20.95"]

How I make this ?

localdata01
  • 587
  • 7
  • 17

2 Answers2

1

Seems like a perfect opportunity for map

arr = arr.map((el) => el.price)

Gives

[ "24.99", "24.95", "20.95" ]

nathan.medz
  • 1,595
  • 11
  • 21
0

Use the Array.map function:

const objarr = [{
    index: 1,
    price: "24.99"
  },
  {
    index: 2,
    price: "24.95"
  },
  {
    index: 3,
    price: "20.95"
  },
]

const arr = objarr.map((element) => element.price);
console.log(arr);
human bean
  • 847
  • 3
  • 15