【问题标题】:How to delete particular elements in a array of objects [closed]如何删除对象数组中的特定元素[关闭]
【发布时间】:2019-07-13 21:00:34
【问题描述】:

我有一个包含多个对象的数组。如何删除一个元素 对象。

例如,我附上了一张我需要删除的图片 组织元素。

请帮我解决。

【问题讨论】:

  • 到目前为止你尝试过什么?我们不会只为您提供解决方案。这不是 StackOverflow 的工作方式。
  • 删除是什么意思?你不想只从你的 json 中获得那个字段

标签: javascript angular


【解决方案1】:

var a = {name: 'pardeep', 'sirname': 'jain'}
delete a['name']
console.log(a);

只需使用 delete 关键字作为这样的键 -

delete objectName['keyName']

【讨论】:

    【解决方案2】:

    使用delete 运算符

    const deleteAttribute = (array, attribute) => array.map(x => {
      // Delete the attribute here
      delete x[attribute];
      return x;
    });
    
    const arr = [{
      attr: 'hello',
      color: 'red',
      organization: 'Good'
    }, {
      attr: 'world',
      color: 'blue',
      organization: 'bad'
    }];
    
    console.log(deleteAttribute(arr, 'organization'));

    【讨论】:

      【解决方案3】:

      您可以使用mapdelete,如下所示

      var arr = [{
        name: 'hi',
        test: 'x',
        organization: 'X'
      }, {
        name: 'guys',
        test: 'y',
        organization: 'Y'
      }];
      
        console.log(arr.map(x => { delete x.organization; return x}));

      【讨论】:

      • 来吧伙计,不要复制/粘贴我的东西。这不酷
      • @molamk 抱歉。我的目的只是帮助他。我在嘲笑一些阵列,但同时得到了你的。所以,我从你那里拿走了阵列。我会改的
      • 没关系,不用担心
      猜你喜欢
      • 1970-01-01
      • 2019-12-12
      • 2018-05-30
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 2018-04-17
      • 2013-01-07
      • 2012-06-15
      相关资源
      最近更新 更多