0

I am working with Javascript/ReactJS code, I am printing the Object(Obj), containing keys as ids and values as the status(true/false) of the corresponding keys in the console. The desired Object is printed, but as soon as I opened that object, it automatically changed its values.

here is code

the first property of this object is true, but when I open it, it goes false.

I declare that Object(Obj) outside the render method because it renders JSX every time either it needed or not. But it is not working.

render(){      
 return defaultApp(templates).map(template=>{
         Obj[`${template._id}`] = false;
        return <Apps id={template._id} 
                  onClick={this.handleToggle.bind(this)}>
                  <h2>{template.title}</h2>
               </Apps>
        });
}
  handleToggle =  (e)=>{
        var   selectedAppId = e.target.id?e.target.id:e.target.parentNode.id;
        if(!isEmpty(Obj)) {
          Object.keys(Obj).forEach((id)=>{
            if(selectedAppId===id){
              Obj[selectedAppId] = !Obj[selectedAppId];
              console.log(Obj);
              this.setState({currentAppId:id,AppsStatus:Obj});
            }});
          }

I want to see the same Object's values either I open it or not.

B4BIPIN
  • 353
  • 2
  • 11
  • 3
    Log a copy of the object: `console.log({...Obj});`. *"but as soon as I opened that object, it automatically changed its values."* yes, that's what the little blue-boxed `i` tells you. The object is shown as it is at the moment you expand the log entry. Hence you should log a copy of it instead. – Felix Kling Apr 14 '19 at 07:20
  • 1
    Related, maybe duplicate: [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/q/4057440/218196) – Felix Kling Apr 14 '19 at 07:26

1 Answers1

0

console JSON.stringify(obj) to check the exact object values.

Remember objects are references so if their key values are changed at any point of the program, the change will be reflected all over the code as all the variables are storing the reference to that object.

Rajan471
  • 338
  • 4
  • 16