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"}
1// To find a specific object in an array of objects
2myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
1//The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
2const array1 = [5, 12, 8, 130, 44];
3
4const found = array1.find(element => element > 10);
5
6console.log(found);
7// expected output: 12
1// MDN Ref:
2// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
3
4var result = jsObjects.find(obj => {
5 return obj.b === 6
6});
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']