Alternative to Object.fromEntries?



我收到这样的object

this.data = {
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匹配,如下所示:

filterOutDeleted(ids: any[], data: object,) {
const remainingItems = Object.fromEntries(Object.entries(data)
.filter(([, item]) => !ids.some(id => id === item.id)));
const rows = Object.keys(remainingItems).map((item) => remainingItems[item]);
return rows;
}

不幸的是,我在构建声明Property 'fromEntries' does not exist on type 'ObjectConstructor'时遇到了一个错误,此时我无法对tsconfig文件进行更改。对于这种情况,是否有fromEntries的替代方案?非常感谢您的帮助!

改为在外部创建对象,对于每个通过测试的条目,手动将其分配给对象。

还要注意,您可以通过提前构建一组ids来降低计算复杂度:

const filterOutDeleted = (ids: any[], data: object) => {
const idsSet = new Set(ids);
const newObj = {};
for (const [key, val] of Object.entries(data)) {
if (!idsSet.has(val.id)) {
newObj[key] = val;
}
}
return newObj;
};

最新更新