gpt4 book ai didi

javascript - 过滤嵌套对象

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

我收到一个如下所示的对象:

 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];
}
});

这取代了 Objectundefined - 这对我的目的有用,但显然不理想。
任何帮助深表感谢!

最佳答案

尝试使用 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));

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

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