0

Trying to find the "age" based on the "name" of the below XML data.

    <data>
        <user>
            <name>Joe</name>
            <age>34</age>
        </user>
        <user>
            <name>Jimmy</name>
            <age>26</age>
        </user>
    </data>

I used xml2json to parse this, returning

{"data":{"user":[{"name":"Joe","age":"34"},{"name":"Jimmy","age":"26"}]}}

With this, how would I be able to get the value 34 by entering in "Joe"?

1 Answer 1

1

Parse the string, then find the record

const json = JSON.parse("...");
const joe = json.data.user.find(x => x.name === "Joe");
console.log(joe.age);
Sign up to request clarification or add additional context in comments.

1 Comment

it would be good to have if(joe) {} else {} to check if Joe is found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.