4

I want to remove property 'b' from all objects how i remove???

 let result=[
    {
       'id':'1',
       'b':'asd'
    },
    {
       'id':'2',
       'b':'asd'
    },
    ...
    ,
    { 
       'id':'2000',
       'b':'asd'
    },
 ]  
  // delete object.b; its only for object and want to remove from whole 
   //  array 2000 records  

Using foreach loop is it correct way to delete key + value pair from array of objects

Aniket kale
  • 609
  • 7
  • 23

3 Answers3

3

if that one is the only structure you have you can do this

result = result.map(e => ({ id: e.id }))

or if the structure is far more complicated, you might want to use delete:

result.forEach((e) => {
    delete e.b;
});
FrontTheMachine
  • 526
  • 3
  • 14
1

You could use forEach.

 let result=[
    {
       'id':'1',
       'b':'asd'
    },
    {
       'id':'2',
       'b':'asd'
    },
    { 
       'id':'2000',
       'b':'asd'
    },
 ];
result.forEach(function(item){ delete item.b });
console.log(result);
lucky
  • 12,734
  • 4
  • 24
  • 46
0

Try following code:

let result=[
    {
       'id':'1',
       'b':'asd'
    },
    {
       'id':'2',
       'b':'asd'
    },
    { 
       'id':'2000',
       'b':'asd'
    }
 ];
 
 for(let i in result){
    let obj = result[i];
    delete obj['b'];
 }
 console.log(result);

Hope it helps :)

jasmin_makasana
  • 472
  • 3
  • 11
  • Yes it Helps me. But Is it right way to do that. because result contain 4000 records. so using it needs for loop and its run 4000 times – Aniket kale Mar 26 '18 at 10:17