0

I have an array of objects in which I have to keep an object always and remove everything else.

columnList = [];

//this object values remains same
defaultObject = {
    key1: value1,
    key2: value2
}
this.columnList.push(defaultObject);

someMethod(){
    this.columnList = []; //emptying array

    //this object's values keep on changing
    anotherObject = {
        key1: value1,
        key2: value2
    }

    this.columnList.push(anotherObject);
}

In the columnList array I need to keep the defaultObject always as first one and replace anotherObject based on some other logic. Here when I clear the array it removes defaultObject as well, which I want to prevent. I only want to remove anotherObject from array when the method call happens. Any suggestions? Thanks.

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
vamsi
  • 1,134
  • 3
  • 20
  • 49
  • 1
    Why not simply: `this.columnList = [defaultObject];` instead of using an empty array? Obviously this depends on whether you still have access to the default object in that context. – Yoshi May 15 '20 at 08:01
  • Wow! it is very simple that I thought. Thanks guys. – vamsi May 15 '20 at 08:05

0 Answers0