JavaScript Найти объект по свойству в массиве
// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
SmokeFrog
// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find(o => o.name === 'string 1');
console.log(obj);
const inventory = [
{name: 'apples', quantity: 2},
{name: 'cherries', quantity: 8}
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
{name: 'cherries', quantity: 15}
];
const result = inventory.find( ({ name }) => name === 'cherries' );
console.log(result) // { name: 'cherries', quantity: 5 }
myArray.findIndex(x => x.id === '45');
myArray.find(x => x.id === '45').foo;
//The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12