-3
const items = [{ price: 250 }, { price: -150 }, { price: 150 }, { price: 500 }];
let result = items.map(({ price }) => price);
console.log(result);

Is it possible to somehow embed a check in this place - items.map(({ price }) => price); - to check for a negative value, or do I need to do it again, map ?

UPD: I want to get summ of all numbers, except negative

spectre_it
  • 107
  • 5

1 Answers1

2

You can do it with .filter() method to get only the objects with negative values

const items = [{ price: 250 }, { price: -150 }, { price: 150 }, { price: 500 }];

const result = items.filter(val => {
  return val.price < 0;
});

console.log(result)

or It can be done like this in one line

const result = items.filter(val => val.price < 0);
Chris G
  • 1,598
  • 1
  • 6
  • 18