0

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
const itemsData = [
  {
    key: 1,
    src: "https://www.davidjones.com/productimages/cart/1/2384359_21556984_6888675.jpg",
    title: "Nautica ANTIGUA PADDED JACKET DARK NAVY",
    price: 245,
    colour: "459 DARK NAVY",
    Size: "M",
    Qty: "1",
  },
  {
    key: 2,
    src: "https://www.davidjones.com/productimages/cart/1/2384359_21556984_6888675.jpg",
    title: "Nautica ANTIGUA PADDED JACKET DARK NAVY",
    price: 245,
    colour: "459 DARK NAVY",
    Size: "M",
    Qty: "1",
  },
  {
    key: 3,
    src: "https://www.davidjones.com/productimages/cart/1/2384359_21556984_6888675.jpg",
    title: "Nautica ANTIGUA PADDED JACKET DARK NAVY",
    price: 245,
    colour: "459 DARK NAVY",
    Size: "M",
    Qty: "1",
  },
];

`

I am trying to filter this array. Just to get a new array as only with price array. Please help me to make a new array that has only price data

const price = [245,245,245]

  • 1
    `itemsData.map(({price}) => price)`. – Hassan Imam May 30 '22 at 09:09
  • Does this answer your question? [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – Hao Wu May 30 '22 at 09:19
  • I'd suggest rephrasing your heading to - how to 'filter', because 'sort' is different and orders the array – Steve Tomlin May 30 '22 at 09:52

3 Answers3

1

You want to use map function on an array and transform the objects inside of it to a new value. Something like this

const itemsData = [
{
key: 1,
src: "https://www.davidjones.com/productimages/cart/1/2384359_21556984_6888675.jpg",
title: "Nautica ANTIGUA PADDED JACKET DARK NAVY",
price: 245,
colour: "459 DARK NAVY",
Size: "M",
Qty: "1",
},
{
key: 2,
src: "https://www.davidjones.com/productimages/cart/1/2384359_21556984_6888675.jpg",
title: "Nautica ANTIGUA PADDED JACKET DARK NAVY",
price: 245,
colour: "459 DARK NAVY",
Size: "M",
Qty: "1",
},
{
key: 3,
src: "https://www.davidjones.com/productimages/cart/1/2384359_21556984_6888675.jpg",
title: "Nautica ANTIGUA PADDED JACKET DARK NAVY",
price: 245,
colour: "459 DARK NAVY",
Size: "M",
Qty: "1",
},
];

const prices = itemsData.map(item => item.price)

console.log(prices)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

szczocik
  • 1,293
  • 1
  • 8
  • 18
0
itemsData.filter(item=>item.key === 1);

You can filter any item in this array like this!

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Hazrat Gafulov
  • 318
  • 4
  • 9
0

It's a simple mapping: itemsData.map(item => item.price)

Also, it's not really React related, more like a Javascript question.

Bálint
  • 1
  • 1