-2

I have object in the format

{
    "room_1": {
        "fan": "on",
        "fan_state": 0,
        "light1": "on",
        "light2": "on",
        "light3": "off",
        "light4": "on"
    },
    "room_2": {
        "fan": "on",
        "fan_state": 2,
        "light1": "off",
        "light2": "on",
        "light3": "on",
        "light4": "off"
    },
    "room_3": {
        "fan": "on",
        "fan_state": 2,
        "light1": "off",
        "light2": "on",
        "light3": "on",
        "light4": "off"
    },
    "room_4": {
        "fan": "on",
        "fan_state": 3,
        "light1": "off",
        "light2": "off",
        "light3": "off",
        "light4": "on"
    }
}

I want to update the value based on the incoming data which is of the (string)format and

{"room_1":{"light1":"off"}}

But i am not able to update it the full object with the updated values what i have tried is to find the key and update it but it wont work

[edit added code]

var jsonRequest=flow.get('post_msg') || {};
var jsonObj = JSON.parse(jsonRequest);
//var jsonObj = jsonRequest;
var js_keys = Object.keys(jsonObj);
//node.warn(jsonRequest);
node.warn("jsonObj " + jsonObj + "  and " + Object.keys(jsonObj) + " type " + typeof (jsonObj));
var oldJSON= msg.payload;
var newJSON = oldJSON;
node.error("1 oldJSON_ payload_newjson " + JSON.stringify(oldJSON) ); 

for (var key in newJSON){
    node.error("for ok: " + key + " " + JSON.stringify(jsonObj[key]) + "" + JSON.stringify(newJSON[key]) + " io " + Object.keys(JSON.parse(jsonObj)) + " val  " + JSON.stringify(Object.values(JSON.parse(jsonObj)) ) );

    if (key == Object.keys(jsonObj)+"" ){
node.warn("true");
    }
        node.error("1ppp " + JSON.stringify(newJSON[key]) ); 
        

       for (var key2 in newJSON[key]){
            node.error("2ppp " + jsonObj + " ok " + JSON.stringify(newJSON[key] )); 
            //if (key2 == Object.keys(JSON.parse(jsonObj) ) ) {

              //  newJSON[key][key2] = JSON.parse(jsonObj[key][key2]);
            //}
        }
    //}

}

//newJSON.room_1.light1 ="off" ;
msg.payload=newJSON ;
var msg_keys = Object.keys(jsonObj);
node.error(msg_keys);
msg.room_no = msg_keys.toString();
msg.inputs =jsonRequest;
//msg.flags= oldJSON.flags;
return  msg;


The code is not working too ..

Please help.

Shariq Azim
  • 136
  • 16

1 Answers1

-1

Please use a library like Lodash for deep assignment.

Note: Be careful when mutating an object. This is just for demonstration. Libraries like React hold immutable states.

const update = {
  "room_1": {
    "light1": "off"
  }
};
  
const main = () => {
  console.log(data.room_1.light1); // >>> on
  _.merge(data, update);           // Mutate the data with deep assignment
  console.log(data.room_1.light1); // >>> off
  console.log(data);               // data.room_1.light1 = "off"
};

const data = {
  "room_1": {
    "fan": "on",
    "fan_state": 0,
    "light1": "on",
    "light2": "on",
    "light3": "off",
    "light4": "on"
  },
  "room_2": {
    "fan": "on",
    "fan_state": 2,
    "light1": "off",
    "light2": "on",
    "light3": "on",
    "light4": "off"
  },
  "room_3": {
    "fan": "on",
    "fan_state": 2,
    "light1": "off",
    "light2": "on",
    "light3": "on",
    "light4": "off"
  },
  "room_4": {
    "fan": "on",
    "fan_state": 3,
    "light1": "off",
    "light2": "off",
    "light3": "off",
    "light4": "on"
  }
};

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Here is an immutable version. The difference here is we merge the original object and the update onto a new object and return that new object.

const update = {
  "room_1": {
    "light1": "off"
  }
};
  
const main = () => {
  console.log(data.room_1.light1);        // >>> on
  const copy = _.merge({}, data, update); // Clone the data with deep assignment
  console.log(data.room_1.light1);        // >>> on  (still the same)
  console.log(copy.room_1.light1);        // >>> off (modified copy)
};

const data = {
  "room_1": {
    "fan": "on",
    "fan_state": 0,
    "light1": "on",
    "light2": "on",
    "light3": "off",
    "light4": "on"
  },
  "room_2": {
    "fan": "on",
    "fan_state": 2,
    "light1": "off",
    "light2": "on",
    "light3": "on",
    "light4": "off"
  },
  "room_3": {
    "fan": "on",
    "fan_state": 2,
    "light1": "off",
    "light2": "on",
    "light3": "on",
    "light4": "off"
  },
  "room_4": {
    "fan": "on",
    "fan_state": 3,
    "light1": "off",
    "light2": "off",
    "light3": "off",
    "light4": "on"
  }
};

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132