find()
方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined
。
var array1 = [5, 12, 8, 130, 44]; var found = array1.find(function(element) { return element > 10; }); console.log(found);
var inventory = [ {name: ‘apples‘, quantity: 2}, {name: ‘bananas‘, quantity: 0}, {name: ‘cherries‘, quantity: 5} ]; function findCherries(fruit) { return fruit.name === ‘cherries‘; } console.log(inventory.find(findCherries)); // { name: ‘cherries‘, quantity: 5 }
原文:https://www.cnblogs.com/ChineseLiao/p/11638175.html