gpt4 book ai didi

javascript - 使用Reactjs中的属性过滤两个数组

转载 作者:行者123 更新时间:2023-12-01 01:33:15 24 4
gpt4 key购买 nike

我有两个数组,想使用reactjs从另一个数组中过滤数组。我只想显示 checked=true 并且第一个数组中的 value 属性等于第二个数组中的 listname

谁能帮忙提供示例代码来做到这一点?

第一个数组:

[
{
"listname": "Cash Deposit",
"totalsuccess": "45"
},
{
"listname": "Cash Withdrawl",
"totalsuccess": "25"
},
{
"listname": "Fund Transfer",
"totalsuccess": "9"
}
]

第二个数组:

[
{
"name": "txn",
"value": "Cash Deposit",
"checked": true
},
{
"name": "txn",
"value": "Cash Withdrawl",
"checked": false
}
]

最佳答案

您可以将filtersome结合使用

const a = [
{
"name": "txn",
"value": "Cash Deposit",
"checked": true
},
{
"name": "txn",
"value": "Cash Withdrawl",
"checked": false
}
]

const b = [
{
"listname": "Cash Deposit",
"totalsuccess": "45"
},
{
"listname": "Cash Withdrawl",
"totalsuccess": "25"
},
{
"listname": "Fund Transfer",
"totalsuccess": "9"
}
]

const res = a.filter(obj => {
if(obj.checked) {
return b.some(item => item.listname === obj.value);
}
return false;
})
console.log(res);

关于javascript - 使用Reactjs中的属性过滤两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53059547/

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