在数组中查找对象
要通过其中一个属性从对象数组中查找对象的话,我们通常使用for
循环:
let inventory = [
{name: 'Bananas', quantity: 5},
{name: 'Apples', quantity: 10},
{name: 'Grapes', quantity: 2}
];// Get the object with the name `Apples` inside the array
function getApples(arr, value) {
for (let index = 0; index < arr.length; index++) {// Check the value of this object property `name` is same as 'Apples'
if (arr[index].name === 'Apples') { //=>原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/271734.html