0

How to find the value from an array of objects. I am accessing the array to get the exact value from @code and #text how to find the values.

const inventory = [
{@code: "5d5a3fdcf417612314982018", #text: "M2-P21 Customer Flow"}
{@code: "5d6e594de40c3c0b1d035f2e", #text: "Customers"}
{@code: "5d760f7939a9a2032306e5c7", #text: "Email test Delete later"}
{@code: "5d973240019db32409761d3e", #text: "Email test Delete later-CLONED"}
{@code: "no_records", #text: "No records"}
{@code: "all_succeeded", #text: "All succeeded"}
{@code: "all_failed", #text: "All Failed"}
{@code: "partially", #text: "Partially Succedded/Failed"}];

const result = inventory.find( ({@code}) => (@code) === '5d6e594de40c3c0b1d035f2e' );
console.log(result);

1
  • Please see the errors in the console Commented Oct 18, 2019 at 9:51

3 Answers 3

2

The inventory object you provide is in bad format.

const inventory = [
{"@code": "5d5a3fdcf417612314982018", "#text": "M2-P21 Customer Flow"},
{"@code": "5d6e594de40c3c0b1d035f2e", "#text": "Customers"},
{"@code": "5d760f7939a9a2032306e5c7", "#text": "Email test Delete later"},
{"@code": "5d973240019db32409761d3e", "#text": "Email test Delete later-CLONED"},
{"@code": "no_records", "#text": "No records"},
{"@code": "all_succeeded", "#text": "All succeeded"},
{"@code": "all_failed", "#text": "All Failed"},
{"@code": "partially", "#text": "Partially Succedded/Failed"}];

const result = inventory.find(item => item["@code"] === "5d6e594de40c3c0b1d035f2e");
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

You need quoted keys and take a quoted key for access.

const inventory = [{ '@code': "5d5a3fdcf417612314982018", '#text': "M2-P21 Customer Flow" }, { '@code': "5d6e594de40c3c0b1d035f2e", '#text': "Customers" }, { '@code': "5d760f7939a9a2032306e5c7", '#text': "Email test Delete later" }, { '@code': "5d973240019db32409761d3e", '#text': "Email test Delete later-CLONED" }, { '@code': "no_records", '#text': "No records" }, { '@code': "all_succeeded", 'text': "All succeeded" }, { '@code': "all_failed", '#text': "All Failed" }, { '@code': "partially", '#text': "Partially Succedded/Failed" }];

const result = inventory.find(o => o['@code'] === '5d6e594de40c3c0b1d035f2e');

console.log(result);

1 Comment

Huge dupe.......
0

Quotes, comma and access via ["@code"]

Here is a findIndex version

const inventory = [
{"@code": "5d5a3fdcf417612314982018", "#text": "M2-P21 Customer Flow"},
{"@code": "5d6e594de40c3c0b1d035f2e", "#text": "Customers"},
{"@code": "5d760f7939a9a2032306e5c7", "#text": "Email test Delete later"},
{"@code": "5d973240019db32409761d3e", "#text": "Email test Delete later-CLONED"},
{"@code": "no_records",               "#text": "No records"},
{"@code": "all_succeeded",            "#text": "All succeeded"},
{"@code": "all_failed",               "#text": "All Failed"},
{"@code": "partially",                "#text": "Partially Succeeded/Failed"}];

const result = inventory[inventory.findIndex(x => x["@code"] === '5d6e594de40c3c0b1d035f2e')]
console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.