facing problems modifying object properties Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of facing problems modifying object properties without wasting too much if your time.

The question is published on by Tutorial Guruji team.

due to some confusion I am not able to manipulate an existing object based on another object while performing inside for-loop. I am using Angular for my project and here is code:

object A

object_a = {

  "status1": {
      "volume": 100,
      "processed": 23,
      "progress": 77
  },

  "status2": {
      "volume": 100,
      "processed": 23,
      "progress": 77
  },

  "status3": {
      "volume": 100,
      "processed": 23,
      "progress": 77
  },

  "status4": {
      "volume": 50,
      "processed": 2,
      "progress": 3
  }

}

object B

object_b = {

  "status1": {
      "volume": 300,
      "processed": 200,
      "progress": 100
  },

}

This is how I am using it:

    let count = 0;
    for(const keyLevel1 in this.object_a){
          let valueLevel1 = this.object_a[keyLevel1];
          let valueObj = {};
          isIncorrect[keyLevel1] = false;
          for(const keyLevel2 in valueLevel1){
            let valueLevel2 = this.object_a[keyLevel1][keyLevel2];
            if(Math.sign(valueLevel2) == -1 || valueLevel2 == "NA"){
              isIncorrect[keyLevel1] = true;
              if(this.object_b !== undefined && 
    Object.keys(this.object_b).length > 0){
    // here i am trying to overwrite                
    objAfterCopy = Object.assign({}, this.object_a, this.object_b[keyLevel1]);
              }
            }else{
              valueObj[keyLevel2] = valueLevel2;
              this.object_b[keyLevel1] = valueObj;
            }
          }
          if (!isIncorrect[keyLevel1]){
            this.object_a[keyLevel1] = state[Object.keys(state)[count]];
            count++;
          }
        }

What the above code does is that it checks for any negative number or “NA” assigned in the number fields and if that is the case then it will assign the correct flag to true. If the number is valid number then it will try to copy that number from object_b and overwrite it to object_a in it’s correct property. I hope this explanation helps.

I want to replace status1 property of object_a with object_b’s.

What I want?

So final output of object_a should be:

object_a = {

  "status1": {
      "volume": 300,
      "processed": 200,
      "progress": 100
  },

  "status2": {
      "volume": 100,
      "processed": 23,
      "progress": 77
  },

  "status3": {
      "volume": 100,
      "processed": 23,
      "progress": 77
  },

  "status4": {
      "volume": 50,
      "processed": 2,
      "progress": 3
  }

}

What I tried

Object.assign({}, this.object_a["status1"], this.object_b["status1"]);

I tried doing like this but then it replaces entire object_a with status1 values, removing all other properties.

I also tried

this.object_a["status1"] = this.object_b["status1"]

but then it throws Cannot assign to read only property

Can somebody please tell me why its not working? I just couldn’t get this to work.

Answer

Try this:

object_a = {...object_a, status1: object_b.status1}

Of course both of them has to be at least const type so:

const object_a and const object_b

or even simpler:

object_a.status1 = {...object_b.status1}
We are here to answer your question about facing problems modifying object properties - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji