1let arr = [
2 { name:"string 1", value:"this", other: "that" },
3 { name:"string 2", value:"this", other: "that" }
4];
5
6let obj = arr.find(o => o.name === 'string 1');
7
8console.log(obj);
1const inventory = [
2 {name: 'apples', quantity: 2},
3 {name: 'cherries', quantity: 8}
4 {name: 'bananas', quantity: 0},
5 {name: 'cherries', quantity: 5}
6 {name: 'cherries', quantity: 15}
7
8];
9
10const result = inventory.find( ({ name }) => name === 'cherries' );
11
12console.log(result) // { name: 'cherries', quantity: 5 }
1const inventory = [
2 {name: 'apples', quantity: 2},
3 {name: 'bananas', quantity: 0},
4 {name: 'cherries', quantity: 5}
5];
6
7function isCherries(fruit) {
8 return fruit.name === 'cherries' && fruit.name === 'bananas';
9}
10
11console.log(inventory.find(isCherries));
12// { name: 'cherries', quantity: 5 }
1function getByValue2(arr, value) {
2
3 var result = arr.filter(function(o){return o.b == value;} );
4
5 return result? result[0] : null; // or undefined
6
7}
8