how can I get value in an object array by a key, which is also in this object array.
the object array looks like this:
const objectArray = [
{key: "1", value: "12321"},
{key: "2", value: "asdfas"}
]
I have now the value of key, e.g. key = 1
, but I want to get 12321 as result.
any solution?
You can use .find() to achieve it.
Try this:
Working Demo
this.objectArray.find(x => x.key == "1").value
To handle exception, if the item doesn't exists in the array, do this:
let item = this.objectArray.find(x => x.key == "1")
this.value = item ? item.value : null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With