0

This is what my array looks like:

const items = [
    { uuid: '123-1234-567', amountMoney: '20,02' },
    { uuid: '111-111-111', amountMoney: '44.04' }
]

And I have the uuid key in the variable:

const uuid = '111-111-111';

Now based on this uuid, I would like to extract the value from the amountMoney: 44.04.

How do you write this in a nice way in js?

reactdto2
  • 123
  • 1
  • 2
  • 7

2 Answers2

0

You can use Array.prototype.find:

items.find(item => item.uuid === uuid) // -> found object
SasoN
  • 152
  • 1
  • 2
  • 11
0

Use Array.prototype.find to find the object if the property uuid of the object matches the value of the variable uuid. Before extracting the value for amountMoney check if the object was found.

Example,

const items = [
    { uuid: '123-1234-567', amountMoney: '20,02' },
    { uuid: '111-111-111', amountMoney: '44.04' }
]

const uuid = '111-111-111';


const foundItem = items.find(item => item.uuid === uuid);

if (foundItem) {
 console.log(foundItem.amountMoney)
}
subashMahapatra
  • 6,339
  • 1
  • 20
  • 26