I have this JavaScript code below which gets me a value from an Array of Objects using an object Key to lookup another object keys value.
JSFiddle Demo http://jsfiddle.net/jasondavis/h5qnwb12/
var selectedId = 2;
var userListArray = [
{
id: 0,
image: "https://www.gravatar.com/avatar/ed1c4f28bbd649aad6c7e1f088ecf3a2?s=32&d=identicon&r=PG&f=1",
name: "None"
},
{
id: 1,
image: "https://www.gravatar.com/avatar/ed1c4f28bbd649aad6c7e1f088ecf3a2?s=32&d=identicon&r=PG&f=1",
name: "User 1",
},
{
id: 2,
image: "https://www.gravatar.com/avatar/ed1c4f28bbd649aad6c7e1f088ecf3a2?s=32&d=identicon&r=PG&f=1",
name: "User 2",
},
{
id: 3,
image: "https://www.gravatar.com/avatar/ed1c4f28bbd649aad6c7e1f088ecf3a2?s=32&d=identicon&r=PG&f=1",
name: "User 3",
},
];
var result = userListArray.filter(function (el) {
return el.id === selectedId;
});
var imageUrl = result[0] && result[0].image;
console.log('Image URL for User '+result[0].name+' === '+imageUrl);
The above demo works just great but in my real app my userListArray
has my id
values wrapped in quotes which breaks the code and makes it return undefined
Demo of my id
wrapped in quotes looks like this...
var userListArray = [
{
id: "0",
image: "https://www.gravatar.com/avatar/ed1c4f28bbd649aad6c7e1f088ecf3a2?s=32&d=identicon&r=PG&f=1",
name: "None"
},
]
is there a way to make my code work when the id is wrapped in quotes?