1

The following is my 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"
        }
    ]
}

This fields are coming from mongoDb and I declared each filed in model. Here I want to delete 'isMater' and '_id' in my array.

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

But its not getting deleted.I am not sure where I made a mistake. Please help.

5
  • 1
    Your code is just working fine....how did you check that it is not working? Commented May 31, 2018 at 5:10
  • You need to be careful with the "tags" you put on your question. If you tag with "javascript" then people with presume you are asking about "JavaScript", no matter what you put into the title. Commented May 31, 2018 at 5:22
  • @Mamum,I just chek my response in a testing tool. Commented May 31, 2018 at 5:27
  • @Neil Lunn,I will take care form now. Commented May 31, 2018 at 5:27
  • Finally,it worked beause I added .lean() to my query.Thank you all for your support. Commented May 31, 2018 at 5:39

3 Answers 3

1

Use array.map() function of JavaScript to do this.

So code will be:

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

Below is complete working code:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Don't use map if you're going to ignore the resulting mapped array - if you just want side effects use forEach instead (as OP is already doing). You're just mutating the original array
@ViJordan,I dont know why but its not working.
1

Shortest way to do this I think.

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

3 Comments

Hi,Nishant its not working for me.
not working for me? Did you check by running above snippet?
Its not your ode fault,i need to put .lean() in my query.Thanks.
0

Try delete the element using key delete map['keyName']; to delete from you object. Like following :

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)

Note: In some JavaScript engine, the delete keyword might hurt performance as it will undo compile / JIT optimization.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.