gpt4 book ai didi

javascript - 过滤对象数组

转载 作者:行者123 更新时间:2023-12-03 05:43:09 24 4
gpt4 key购买 nike

我有一个像这样的数组:

const xx = [
{
name: "Alex",
income: 324300,
total: 3030000
},
{
name: "Snake",
income: 3433000,
total: 34323000
}
];

我想检查收入是否大于 300000,如果是,那么他们的名字将被存储或记录。

这是我尝试过的,我不完全理解它是如何工作的..

var f = {};
for (var i = 0, len = xx.length; i < len; i++) {
f[xx[i].id] = xx[i];
if(f[income]>=500000){
console.log(xx[i]);
}
}

最佳答案

您可以使用 Array.filter 来过滤数组,并使用 Array.forEach 将其迭代到 console.log 或您想要对其执行的操作。

const xx = [
{
name: "Alex",
income: 324300,
total: 3030000
},
{
name: "Snake",
income: 3433000,
total: 34323000
},
{
name: "Wake",
income: 4,
total: 3
}
];

xx.filter(function(x) {
return x.income > 30000;
}).forEach(function(x){
console.log(x)
});

关于javascript - 过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40439985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com