javascript - 使用 Reactjs 中的属性过滤两个数组
问题描述
我有两个数组,想用 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
}
]
解决方案
你可以使用filter
withsome
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);
推荐阅读
- blender - Blender 网格数据结构
- qt - Qt Quick Controls 2:检索组件的标准值
- javascript - 单击按钮时如何创建烟花
- c# - 从其他文本框绑定文本框
- namespaces - 如何在 ClojureScript 中运行 eval 并访问调用 eval 的命名空间?
- c# - C# 使用单个按钮运行 3 个 MYSQL 插入查询 - 来自文本框和列表视图的数据
- python - 如何在 Tensorboard 中通过 Eager Execution 可视化 keras 卷积过滤器
- azure - 如何在不注销用户的情况下交换插槽
- c - MPI_Gather 只有 1 个等级
- java - Java 8 功能接口命名约定?