如何在JavaScript中从数组对象中删除特定的键和值?

8

这里我有一个名为arrayData的数组对象,其中包含多个对象。我想从这个arrayData中删除索引和类型键值,如何删除?

arrayData : [
  0: {
       index: 0
       is_required: true
       name: "vmvdnksl"
       type: "LONG_TEXT"
     }
  1: {
       index: 1
       is_required: true
       name: "dsvnlk"
       type: "MULTIPLE_SELECTORS"
     }
   ]

在删除索引和类型之后,我希望得到这种类型的结果。

 arrayData : [
  0: {
       is_required: true
       name: "vmvdnksl"
     }
  1: {
       is_required: true
       name: "dsvnlk"
     }
   ]

1
可能是如何在Javascript中完全从数组中删除对象?的重复问题。 - Nithya Rajan
请更新您期望的结果。我假设您希望保留相同的结构,但不显示“index”和“type”,这是正确的吗? - Shidersz
@Shidersz,我更新了我的问题。 - user10950990
好的,还有一件事,arrayData是一个数组还是一个对象? - Shidersz
@Shidersz arrayData 是一个数组。 - user10950990
6个回答

30
您可以使用rest参数。当您有许多要保留并仅删除其中一些的键时,它会非常有用。

const arrayData= [{index: 0,is_required: true,name: "vmvdnksl",type: "LONG_TEXT"},{index: 1,is_required: true,name: "dsvnlk",type: "MULTIPLE_SELECTORS"}];

const result = arrayData.map(({type,index,...rest}) => ({...rest}));

console.log(result);


如果有一个键的数组而不是单独的键,我该如何编写映射? - minigeek
@minigeek,你能否请发一个你所说的例子?顺便提一下,你也可以解构数组,参考 https://dev59.com/F1QJ5IYBdhLWcg3wFx1_ - Code Maniac
是的,我尝试过了...无法提出正确的逻辑。基本上,我有一个字符串数组,其中包含已知键“ type”和“ index”。而不是类型,索引为已知键。 我将其作为输入传递给我的 Angular 组件。如果我们知道键,则您的答案是最好的方法,但现在我必须使用删除和过滤器组合来进行操作。 - minigeek

5
你可以使用 Array.map()解构 来完成这个任务:

const arrayData = [
    {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
     {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
];

let res = arrayData.map(({is_required, name}) => ({is_required, name}));
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

我更倾向于不改变原始数据,但如果需要这样做,可以采用以下方式(或使用其他人展示的delete方法):

const arrayData = [
    {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
     {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
];

let res = arrayData.forEach(
    ({is_required, name}, idx, arr) => arr[idx] = ({is_required, name})
);
console.log(arrayData);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}


1
有没有可能使用索引和类型而不是 is_required 或 name 来移除使用类型和索引? - user10950990
1
@Akki,请检查Code Maniac给出的答案,或者像其他答案所示,使用delete提示:为了获得您的问题的投票,请下次提供您已经尝试过的内容。 - Shidersz

1
你可以使用delete关键字从对象中删除属性。

var arrayData = [
  0: {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
  1: {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
 ];

for (var i in arrayData) {
  for (var j in arrayData[i]) {
    if (j === 'index' || j === 'type') {
      delete arrayData[i][j];
    }
  }
}

console.log(arrayData);


@Akki 我已经这样做了。你能否在我的问题下面按下检查标记并投票支持它呢?谢谢! - Vappor Washmade

1
对于数组,您可以使用 map() 函数。

    var arrayData = [
      {
           index: 0,
           is_required: true,
           name: "vmvdnksl",
           type: "LONG_TEXT"
      },
      {
           index: 1,
           is_required: true,
           name: "dsvnlk",
           type: "MULTIPLE_SELECTORS"
      }
    ],
    mappedArrayData = arrayData.map(({is_required, name}) => {
       return {is_required, name};
    })
    
    console.log(mappedArrayData);

要删除对象,请使用delete运算符。

var arrayData = {
  0: {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
  1: {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
  };
  
  
for (let key in arrayData) {
  delete arrayData[key].index;
  delete arrayData[key].type;
}
  
console.log(arrayData);


0

您可以简单地使用 Array.map() 来显示所需的属性:

const arrayData= [
   {
       index: 0,
       is_required: true,
       name: "vmvdnksl",
       type: "LONG_TEXT"
     },
   {
       index: 1,
       is_required: true,
       name: "dsvnlk",
       type: "MULTIPLE_SELECTORS"
     }
   ];

const result = arrayData.map(({is_required, name}) => ({is_required, name}));

console.log(result);


有没有可能使用索引和类型而不是 is_required 或 name 来移除使用类型和索引? - user10950990
这很罕见,最后一个答案清楚地展示了许多其他答案已经展示的相同代码,为什么会得到更多的赞? - Shidersz

0

尝试使用JavaScript的删除运算符,如下面的代码示例所示:

for(let o in arrayData){
    delete arrayData[o].index;
    delete arrayData[o].type
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接