【问题标题】:How to remove an element from an array in js [Elements are from mongodb] [duplicate]如何从js中的数组中删除元素[元素来自mongodb] [重复]
【发布时间】:2018-11-10 00:50:42
【问题描述】:

以下是我的数组,

[
  {
    "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;
    }
});

但它没有被删除。我不确定我在哪里犯了错误。请帮忙。

【问题讨论】:

  • 您的代码运行良好....您如何检查它是否无法运行?
  • 您需要小心您在问题上放置的“标签”。如果您使用“javascript”进行标记,那么无论您在标题中添加什么内容,人们都会认为您在询问“JavaScript”。
  • @Mamum,我只是在测试工具中查看我的回复。
  • @Neil Lunn,我现在会照顾好表格。
  • 最后,它成功了,因为我在查询中添加了 .lean()。谢谢大家的支持。

标签: arrays node.js mongodb mongoose


【解决方案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);

【讨论】:

  • 如果您要忽略生成的映射数组,请不要使用 map - 如果您只是想要副作用,请改用 forEach (正如 OP 已经在做的那样)。你只是在改变原始数组
  • @ViJordan,我不知道为什么,但它不起作用。
【解决方案2】:

我认为最短的方法。

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

【讨论】:

  • 嗨,它不适合我。
  • 不适合我?您是否通过在 sn-p 上运行来检查?
  • 这不是你的 ode 错误,我需要将 .lean() 放入我的查询中。谢谢。
【解决方案3】:

尝试使用键 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 优化。

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2011-12-31
    • 2017-12-22
    • 1970-01-01
    • 2021-02-18
    • 2011-07-03
    • 2015-06-12
    • 2013-01-06
    相关资源
    最近更新 更多