I have object and I would like to get the value of the id somehow by providing type = “EXTRA”. Could someone help with this?
{ "packages": [ { "id": "123", "type": "EXTRA", "name": "text", "extras": [ { "test": "1", "test": "2" } ] }, { "id": "234", "type": "BASE", "name": "text2", "extras": [], } ] }
Advertisement
Answer
Your array
var arr = { "packages": [ { "id": "123", "type": "EXTRA", "name": "text", "extras": [ { "test": "1", "test": "2" } ] }, { "id": "234", "type": "BASE", "name": "text2", "extras": [], } ] }
You can use the find() method for what you need providing the ‘EXTRA’ value of type
var result = arr.packages.find(x => x.type === 'EXTRA');
Your Output
console.log(result.id); //123