0

Why delete is working in another data ?

      this.Line = [];
      this.showDataForm = [{
        'name': 'BTC',
        'data': [1,2,3,4]
        'chart': 'linemulti'
      }]

      for (var index=0; index < this.showDataForm.length; index++) {
        if ( this.showDataForm[index].chart === "linemulti" ) { // for merge chart
          this.Line.push(this.showDataForm[index]);
        }
      }
      
      for (var key=0; key < this.Line.length; key++){
          delete this.Line[key].chart;
      }

I try to show:

console.log(this.showDataForm);

[{'name':'BTC','data':[1,2,3,4]}]

why delete is working in this.showDataForm ? how to fix ?

user14324808
  • 43
  • 1
  • 6

1 Answers1

1

When you call delete on the item in the Line array, it is just a reference to the original object in showDataForm.

If you wanted to instead copy a new item to the Line array you can use the spread operator ... as is shown below. This means the original object is not affected when you delete one of the keys:

this.Line = [];
this.showDataForm = [{
  'name': 'BTC',
  'data': [1,2,3,4],
  'chart': 'linemulti'
}]

for (var index=0; index < this.showDataForm.length; index++) {
  if ( this.showDataForm[index].chart === "linemulti" ) { // for merge chart
    this.Line.push({...this.showDataForm[index]});
  }
}

for (var key=0; key < this.Line.length; key++){
    delete this.Line[key].chart;
}

console.log(this.showDataForm)
Jamiec
  • 133,658
  • 13
  • 134
  • 193