Object.fromEntries 的替代方案?

suu*_*iam 4 javascript object typescript

我收到object这样的消息:

this.data = {
    O: {
        id: 0,
        name: value1,
        organization: organization1,
        ...,
       },
    1: {
        id: 1,
        name: value1,
        organization: organization1,
        ...,
        },
    2: {
        id: 2,
        name: value2,
        organization: organization2,
        ...,
        },
    ...
   } 
Run Code Online (Sandbox Code Playgroud)

然后,我过滤id并删除与我从商店收到的匹配的Object内容,如下所示:idid

  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;
  }
Run Code Online (Sandbox Code Playgroud)

不幸的是,我在构建说明时遇到错误,并且目前Property 'fromEntries' does not exist on type 'ObjectConstructor'无法在文件中进行更改。对于这种情况tsconfig有替代方案吗?fromEntries任何帮助深表感谢!

Cer*_*nce 5

相反,在外部创建对象,并且对于通过测试的每个条目,手动将其分配给该对象。

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;
};
Run Code Online (Sandbox Code Playgroud)