I have a json that, for example lets say looks like this:
{
{
"name":"Foo"
"nickname":"Lorem Ipsum"
},
{
"name":"Bar"
"nickname":"Dolor Sit"
}
}
Now, i want to find the nickname of something using the name to find it. Is this possible in JavaScript?
CodePudding user response:
Given the input should be corrected in a way pointed out in my comment to be a proper and correct JSON, this function will serve the purpose.
function findByName(name) {
const i = [{
"name": "Foo",
"nickname": "Lorem Ipsum"
},
{
"name": "Bar",
"nickname": "Dolor Sit"
}
]
return i.find(e => e.name === name).nickname;
}
const res = findByName("Foo"); // ==> "Lorem Ipsum"
console.log(res);