【问题标题】:remove all properties with the same key name inside object [duplicate]删除对象内具有相同键名的所有属性[重复]
【发布时间】:2020-01-22 17:18:10
【问题描述】:

假设我的对象看起来像这样

{
  "success": true,
  "result": 1,
  "id": "123",
  "group": [
    {
      "id": "333",
      "label": "group 1",
      "students": [
        {
          "id": "s123",
          "name": "ying"
        },
        {
          "id": "s456",
          "name": "yang"
        }
      ]
    },
    {
      "id": "888",
      "label": "group 2",
      "students": [
        {
          "id": "j123",
          "name": "yinga"
        },
        {
          "id": "j456",
          "name": "yanga"
        }
      ]
    },
  ]
}

我需要删除所有带有键“id”的属性。. 表示根对象内的ids,组和学生需要完全删除。

如何在 Node 或 lodash 中做到这一点?

【问题讨论】:

标签: node.js lodash


【解决方案1】:

let data = {
  "success": true,
  "result": 1,
  "id": "123",
  "group": [
    {
      "id": "333",
      "label": "group 1",
      "students": [
        {
          "id": "s123",
          "name": "ying"
        },
        {
          "id": "s456",
          "name": "yang"
        }
      ]
    },
    {
      "id": "888",
      "label": "group 2",
      "students": [
        {
          "id": "j123",
          "name": "yinga"
        },
        {
          "id": "j456",
          "name": "yanga"
        }
      ]
    },
  ]
}

let {id, ...prunedData} = {
  ...data,
  group: !!data.group ? data.group.map(g=>{
    let {id, ...withoutGroupId} = g
    return {
      ...withoutGroupId,
      students: !!g.students ? g.students.map(s=>{
        let {id, ...withoutStudentId} = s;
        return withoutStudentId;
      }) : null
    }
  }) : null
} ;

console.log(prunedData);

【讨论】:

  • 我试过你的代码,但得到了错误TypeError: Cannot read property 'map' of undefined,它指向group: data.group.map(g=>{这一行
  • groupstudents 不是数组时会发生这种情况。你没有提到他们可能是空的。我会编辑
  • 谢谢,但我已经从上面的链接中得到了答案
  • @imin 我确实更新了它。玩得开心,尝试学习js/es。
【解决方案2】:

这应该可行:

const myObject = {
  success: true,
  result: 1,
  id: "123",
  group: [
    {
      id: "333",
      label: "group 1",
      students: [
        {
          id: "s123",
          name: "ying"
        },
        {
          id: "s456",
          name: "yang"
        }
      ]
    },
    {
      id: "888",
      label: "group 2",
      students: [
        {
          id: "j123",
          name: "yinga"
        },
        {
          id: "j456",
          name: "yanga"
        }
      ]
    }
  ]
};

function removeKeyFromObject(keyToDelete, objectToParse) {
  if (!objectToParse) {
    return objectToParse
  }

  if (Array.isArray(objectToParse)) {
    return objectToParse.map(objectInArray => {
      return removeKeyFromObject(keyToDelete, objectInArray);
    });
  } else if (objectToParse && typeof objectToParse === "object") {
    if (keyToDelete in objectToParse) {
      delete objectToParse[keyToDelete];
    }
    const newObject = {};

    Object.keys(objectToParse).forEach(currentKey => {
      if (currentKey === keyToDelete) {
        return;
      }
      if (typeof objectToParse[currentKey] === "object") {
        return (newObject[currentKey] = removeKeyFromObject(
          keyToDelete,
          objectToParse[currentKey]
        ));
      }
      return (newObject[currentKey] = objectToParse[currentKey]);
    });
    return newObject;
  }
}

console.log(JSON.stringify(removeKeyFromObject("id", myObject), null, 2));

【讨论】:

  • 我尝试了您的代码,但出现错误`TypeError: Cannot use 'in' operator to search for 'id' in null` 指向此行if (keyToDelete in objectToParse)
  • 立即尝试。此外,如果您发布您正在使用的实际数据,这将很有帮助。您粘贴的数据结构通过了我的代码。你有实际使用的数据结构来运行它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 2021-06-25
  • 2019-12-23
  • 1970-01-01
  • 2015-12-14
相关资源
最近更新 更多