JSFiddle - React, Tailwind, and code Playground
by rwaldron
JavaScript
var myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}];
function findById(source, id) {
return source.filter(function( obj ) {
// coerce both obj.id and id to numbers
// for val & type comparison
return +obj.id === +id;
})[ 0 ];
}
var result = findById( myArray, 45 );
console.log(
result.foo // "bar"
);