javascript - 过滤嵌套对象
问题描述
我收到一个如下所示的对象:
this.tokensData = {
O: {
id: 0,
name: value1,
organization: organization1,
...,
},
1: {
id: 1,
name: value1,
organization: organization1,
...,
},
2: {
id: 2,
name: value2,
organization: organization2,
...,
},
...
}
我想过滤id
并删除Object
与我id
从. 到目前为止我尝试了什么:id
store
const filteredObject = Object.keys(this.tokensData).map((token) => {
if (this.$store.state.id !== this.tokensData[token].id) {
return this.tokensData[token];
}
});
这取代了Object
-undefined
这将适用于我的目的,但显然并不理想。任何帮助深表感谢!
解决方案
尝试使用Object.entries
然后Object.fromEntries()
从键值对列表中创建一个对象:
let store = [0 , 1];
const result = Object.entries(tokensData).filter(([k, v]) => !store.some(s => s == v.id));
console.log(Object.fromEntries(result));
一个例子:
let tokensData = {
O: {
id: 0,
name: '',
organization: '',
},
1: {
id: 1,
name: '',
organization: '',
},
2: {
id: 2,
name: '',
organization: '',
}
}
let store = [0 , 1];
const result = Object.entries(tokensData).filter(([k, v]) => !store.some(s => s == v.id));
console.log(Object.fromEntries(result));
推荐阅读
- social-networking - 未来的社交网络网站和应用程序
- javascript - 将值推入数组
- r - 如何平滑ggplot中密度图的曲线?
- mysql - 包含排名数字和 DNF(未完成)或 DNS(未开始)的运动排名列的数据库设计
- siddhi - 是否可以在 siddhi 查询语言中集成模糊性?
- r - 在 R 的循环中使用“count”函数
- reactjs - React js Material Ui TextField默认值不会改变
- linkedin - 如何找到linkedIn的所有国家URN列表?
- excel - VBA比较行并突出显示同一工作表中的差异
- amazon-web-services - AWS CloudFront 的集中式多账户日志记录