0

I have this Array Object that looks like this:

let arr = [
 {Name: "sub", Value: "ababbnnn"}
 ]

I'm trying to access the value of the Name custom:network meaning I want to output this: abcdef1233bfgh. So far I have this loop but I wonder if there may be a cleaner way. Thanks a lot in advance. Here's my code:

Devmix
  • 1,599
  • 5
  • 36
  • 73
  • 1
    [Don't use `for…in` enumerations on arrays!](https://stackoverflow.com/q/500504/1048572) – Bergi Mar 14 '19 at 19:29

1 Answers1

6

You can use the find method:

const value = arr.find(item => item.Name === "custom:network").Value

To cover the case where no item is returned by find, you can use the following approach:

const value = (arr.find(item => item.Name === "custom:network") || {}).Value
Sam Herrmann
  • 6,293
  • 4
  • 31
  • 50