如何从js中删除数组中的元素[元素来自mongodb]

时间:2018-05-31 05:06:39

标签: arrays node.js mongodb mongoose

以下是我的数组

[
  {
    "key_id": "#K000030",
    "_id": "K000030",
    "isMaster": true,
    "specialties": [
        {
            "speciality1_count": "x039",
            "speciality1": "Infectious Disease"
        }
    ]
},
{
    "key_id": "#K000442",
    "_id": "K000442",
    "keyword": "Artificial Heart Ventricle",
    "isMaster": true,
    "specialties": [
        {
            "speciality1_count": "x011",
            "speciality1": "Cardiothoracic Surgery"
        },
        {
            "speciality2_count": "x041",
            "speciality2": "Interventional Cardiology"
        }
    ]
}

此字段来自mongoDb,我在模型中声明了每个字段。在这里,我想删除' isMater'和' _id'在我的阵列中。

resp.forEach(function(v,k){
    if (v) {
      delete v.isMaster;
      delete v._id;
    }
});

但它没有被删除。我不知道我犯了什么错误。请帮忙。

3 个答案:

答案 0 :(得分:1)

使用JavaScript的array.map()功能执行此操作。

所以代码将是:

array.map(function(item) {
  delete item._id;
  delete item.isMaster;
  return item;
});

以下是完整的工作代码:

let array = [{"key_id":"#K000030","_id":"K000030","isMaster":true,"specialties":[{"speciality1_count":"x039","speciality1":"Infectious Disease"}]},{"key_id":"#K000442","_id":"K000442","keyword":"Artificial Heart Ventricle","isMaster":true,"specialties":[{"speciality1_count":"x011","speciality1":"Cardiothoracic Surgery"},{"speciality2_count":"x041","speciality2":"Interventional Cardiology"}]}];

array.map(function(item) {
  delete item._id;
  delete item.isMaster;
  return item;
});

console.log(array);

答案 1 :(得分:1)

我认为最简单的方法。

data = data.map(({_id,isMaster,...val})=>val);

var data  =[
  {
    "key_id": "#K000030",
    "_id": "K000030",
    "isMaster": true,
    "specialties": [
        {
            "speciality1_count": "x039",
            "speciality1": "Infectious Disease"
        }
    ]
},
{
    "key_id": "#K000442",
    "_id": "K000442",
    "keyword": "Artificial Heart Ventricle",
    "isMaster": true,
    "specialties": [
        {
            "speciality1_count": "x011",
            "speciality1": "Cardiothoracic Surgery"
        },
        {
            "speciality2_count": "x041",
            "speciality2": "Interventional Cardiology"
        }
    ]
}];

data = data.map(({_id,isMaster,...val})=>val);

console.log(data);

答案 2 :(得分:0)

尝试使用键delete map['keyName'];删除元素以从对象中删除。如下:

let resp = [{
    "key_id": "#K000030",
    "_id": "K000030",
    "isMaster": true,
    "specialties": [{
      "speciality1_count": "x039",
      "speciality1": "Infectious Disease"
    }]
  },
  {
    "key_id": "#K000442",
    "_id": "K000442",
    "keyword": "Artificial Heart Ventricle",
    "isMaster": true,
    "specialties": [{
        "speciality1_count": "x011",
        "speciality1": "Cardiothoracic Surgery"
      },
      {
        "speciality2_count": "x041",
        "speciality2": "Interventional Cardiology"
      }
    ]
  }
];

resp.forEach(function(v,k){
   if(v){
      delete v['isMaster'];
      delete v['_id'];
   }
});

console.log(resp)

注意:在某些JavaScript引擎中,delete关键字可能会影响性能,因为它会撤消编译/ JIT优化。