0

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

Nimmi
  • 1,869
  • 2
  • 10
  • 20
  • I appreciate your quick response but I tried that and it is not working for me (gives me error related to function). Can you please help me by creating some fiddle with my requirement. Thanks in advance. – Nimmi Apr 23 '18 at 14:51
  • you commented on your question, may comment on the answer? – Jonas Wilms Apr 23 '18 at 14:55
  • in https://stackoverflow.com/questions/34698905/clone-a-js-object-except-for-one-key clone an object without a key, you can map your array using the function – Eliseo Apr 23 '18 at 14:55

2 Answers2

2
  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);
  }
Eliseo
  • 29,422
  • 4
  • 13
  • 37
1

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;
});
gurvinder372
  • 61,170
  • 7
  • 61
  • 75