【问题标题】:How to delete an item from an object of ids如何从 ids 对象中删除项目
【发布时间】:2019-12-27 05:56:28
【问题描述】:

const comments = {
  '1': { id: '1', text: 'please add code examples' },
  '2': { id: '2', text: 'examples would be great for this article' },
  '3': { id: '3', text: 'hi there' },
};

const removeItem = items => {
  delete items[1];
};

console.log(removeItem(comments));

我需要根据其唯一标识符删除一个项目。上面写着comments is already declared

【问题讨论】:

  • 您的代码运行良好。它记录undefined,因为您正在记录removeItem 的结果(它不返回任何内容),但是如果您在removeItem(comments) 之后记录comments,您会看到第一项不再存在。您所描述的 "comments is already declared" 错误在您共享的代码中不存在。
  • Console.log 在您调用 removeItem 后退出 cmets 数组,它会显示您想要的样子

标签: javascript object ecmascript-6


【解决方案1】:

如果你想删除一个特定的 id,并且我希望你的对象键与它的 id 相同,你可以改变删除项以接受 id 参数并稍微修改函数以便它会根据key属性删除对象。

var comments = {
    1: { id: '1', text: 'please add code examples' },
    2: { id: '2', text: 'examples would be great for this article' },
    3: { id: '3', text: 'hi there' },
  };

var removeItem = (items, id) => {
    delete items[id];
};

removeItem(comments, 1);

console.log(comments);

【讨论】:

    【解决方案2】:

    使用 delete 就像你所做的那样,这正是你想要做的 - 但如果你对为什么得到 undefined 感到困惑,那是因为你没有从 @987654323 返回任何东西@ 功能。尝试返回items:

    const comments = {
      '1': { id: '1', text: 'please add code examples' },
      '2': { id: '2', text: 'examples would be great for this article' },
      '3': { id: '3', text: 'hi there' },
    };
    
    const removeItem = items => {
      delete items[1];
      return items;
    };
    
    console.log(removeItem(comments));

    【讨论】:

      【解决方案3】:

      您的代码运行良好,但您需要在完成工作后通过控制台注销正确的事情。

      const comments = {
        '1': { id: '1', text: 'please add code examples' },
        '2': { id: '2', text: 'examples would be great for this article' },
        '3': { id: '3', text: 'hi there' },
      };
      
      const removeItem = items => {
        delete items[1];
      };
      
      removeItem(comments);
      
      console.log(comments);
      

      运行 removeItem 后,您的 cmets 数组如下所示:

      comments
          2: {id: "2", text: "examples would be great for this article"}
          3: {id: "3", text: "hi there"}
      

      【讨论】:

      • 删除项目功能的设置方式只会删除 1 键中的项目。
      • 这是被问到的
      【解决方案4】:

      有两种方法可以做到这一点:

      A) 仅当您想从 comments(变量或常量)中删除项目时

      var comments = {
          '1': { id: '1', text: 'Text A' },
          '2': { id: '2', text: 'Text B' },
          '3': { id: '3', text: 'Text C' },
        };
      var removeComment = (n) => {
          // validation or check
          delete comments[n];
      };
      console.log(removeComment(1));
      

      B) 如果你想让它把一个对象作为参数和索引 (n):

      var comments = {
          '1': { id: '1', text: 'Text A' },
          '2': { id: '2', text: 'Text B' },
          '3': { id: '3', text: 'Text C' },
        };
      var removeItem = (items, n) => {
          delete items[n];
      };
      console.log(removeItem(comments, 1));
      

      您已经声明了错误,因为您在全局命名空间中的代码中的某处将comments 用于其他常量或变量。尝试使用var(变量)而不是const,上面的代码应该适合你。

      【讨论】:

        【解决方案5】:

        您可以创建一个函数来获取 cmets 和想要的评论 ID,然后如果找到它会删除该评论。

        const comments = {
            '1': {
              id: '1',
              text: 'please add code examples'
            },
            '2': {
              id: '2',
              text: 'examples would be great for this article'
            },
            '3': {
              id: '3',
              text: 'hi there'
            },
          },
          removeItem = (cmts, id) => {
            cmts[id] && (delete cmts[id]);
            return cmts;
          };
        console.log(removeItem(comments, 3)); /** deletes the comment with ID=3 **/
        console.log('*** second call ****')
        console.log(removeItem(comments, 55)); /** nothing happens and the same data is returned **/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-10
          • 1970-01-01
          • 1970-01-01
          • 2021-08-01
          • 2018-10-15
          相关资源
          最近更新 更多