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]

3
  • 1
    itemsData.map(({price}) => price). Commented May 30, 2022 at 9:09
  • Does this answer your question? From an array of objects, extract value of a property as array Commented May 30, 2022 at 9:19
  • I'd suggest rephrasing your heading to - how to 'filter', because 'sort' is different and orders the array Commented May 30, 2022 at 9:52

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

0
itemsData.filter(item=>item.key === 1);

You can filter any item in this array like this!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.