Nimmi
Nimmi

Reputation: 2017

Remove specific property from an array of object

enter image description hereI have issue with splicing item from an array object with specific property. Here is the explanation.

  delColumn($event: any, el: any) {
if ($event.target && el > -1) {
  var colId: string = this.receivedData[0].value.columns[el].id;
  var obj = this.receivedData[0];
  obj.values.columns = obj.values.columns.filter(s => s.id != colId);
  obj.values.rows.forEach(s => {
    delete s.Col_1;
    return s;
  });
}

}

Now, My requirement is When I click on Delete Column it comes into this method and I want to delete specific column and rows associated with that.

Thanks for the help in advance.

enter image description here

Upvotes: 0

Views: 162

Answers (2)

Eliseo
Eliseo

Reputation: 57929

  rows=[{Col1:1,Value:1},
        {Col1:1,Value:2},
        {Col1:1,Value:3},
        {Col1:1,Value:4}];

  rowsNew=this.rows.map(x=>{
     return this.objectWithoutKey(x,"Col1");
  })
  objectWithoutKey(object, key){
    const {[key]: deletedKey, ...otherKeys} = object;
    return otherKeys;
  }
  ngOnInit()
  {
     console.log(this.rows);
     console.log(this.rowsNew);
  }

Upvotes: 2

gurvinder372
gurvinder372

Reputation: 68393

Use filter and forEach

obj.values.columns = obj.values.columns.filter(s => s.id != "Col_1");
obj.values.rows.forEach( s => {
   delete s.Col_1;
   return s;
});

Upvotes: 2

Related Questions