1const jsObjects = [
2 {id: 1, displayName: "First"},
3 {id: 2, displayName: "Second"},
4 {id: 3, displayName: "Third"},
5 {id: 4, displayName: "Fourth"}
6]
7
8// You can use the arrow function expression:
9var result = jsObjects.find(obj => {
10 // Returns the object where
11 // the given property has some value
12 return obj.id === 1
13})
14
15console.log(result)
16
17// Output: {id: 1, displayName: "First"}
1const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
2
3const filterItems = (needle, heystack) => {
4 let query = needle.toLowerCase();
5 return heystack.filter(item => item.toLowerCase().indexOf(query) >= 0);
6}
7
8console.log(filterItems('ap', fruits)); // ['apple', 'grapes']
9console.log(filterItems('ang', fruits)); // ['mango', 'orange']
1// Find an object with a given property in an array
2const desiredObject = myArray.find(element => element.prop === desiredValue);