There was a question from one of the user with the below problem.
Problem
There is an Typescript Array and he wanted to fetch a particular object based on the attribute without iterating through the array.
Here is the simple solution
var objArray = [ { id: 0, name: 'Object 0', otherProp: '321' }, { id: 1, name: 'O1', otherProp: '648' }, { id: 2, name: 'Another Object', otherProp: '850' }, { id: 3, name: 'Almost There', otherProp: '046' }, { id: 4, name: 'Last Obj', otherProp: '984' }];To fetch the object which has id as 4 var result =objArray.find(obj => {return obj.id === 4})This would return you the result object as { id: 4, name: 'Last Obj', otherProp: '984' }
Comments
Post a Comment