TypeScript
Tableaux
Recherche…
Recherche d'objet dans un tableau
Utiliser find ()
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function findCherries(fruit) {
return fruit.name === 'cherries';
}
inventory.find(findCherries); // { name: 'cherries', quantity: 5 }
/* OR */
inventory.find(e => e.name === 'apples'); // { name: 'apples', quantity: 2 }
Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow