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

时间:2019-03-06 05:17:15

标签: javascript

这里我在arrayData中有arrayData数组对象,我有多个对象,我想从该数组对象中删除索引并键入键值,如何从此arrayData中删除?

// ...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "startVC")
    self.present(vc, animated: false, completion: nil)

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

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

6 个答案:

答案 0 :(得分:2)

  

您可以使用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);

答案 1 :(得分: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);

答案 2 :(得分:0)

您可以使用Array.map()destructuring来完成此任务:

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;}

答案 3 :(得分:0)

尝试使用JavaScript的delete operator,如以下代码示例所示:

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

答案 4 :(得分:0)

对于数组,您可以使用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);

答案 5 :(得分: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);