1

here is my problem, reported in a way much more simplified

this is the json (look at the json here if you want)

{"resource":[{"id":"1408694994","obj":[{"id":"1","action":[{"name":"ON","id":"301"},{"name":"OFF","id":"302"}]},{"id":"2","action":[{"name":"ON","id":"303"},{"name":"OFF","id":"304"}]}]},{"id":"1408694995","obj":[{"id":"3","action":[{"name":"ON","id":"305"},{"name":"OFF","id":"306"}]},{"id":"4","action":[{"name":"ON","id":"307"},{"name":"OFF","id":"308"}]}]}]}

Given an id (in this case we say id = 3), I must save the item with that ID. And i save in a new object (newobj) the object with that ID, but changing the array ACTION in an OBJ with the only action ON

Example

<script> 

    var tt = '{"resource":[{"id":"1408694994","obj":[{"id":"1","action":[{"name":"ON","id":"301"},{"name":"OFF","id":"302"}]},{"id":"2","action":[{"name":"ON","id":"303"},{"name":"OFF","id":"304"}]}]},{"id":"1408694995","obj":[{"id":"3","action":[{"name":"ON","id":"305"},{"name":"OFF","id":"306"}]},{"id":"4","action":[{"name":"ON","id":"307"},{"name":"OFF","id":"308"}]}]}]}';

    var myjson = JSON.parse(tt);

    var search = 3;


    console.log(myjson.resource[1].obj[0]);

    for(var i = 0 ; i < myjson.resource.length; i++){

        for(j = 0 ; j < myjson.resource[i].obj.length; j++){

            if(parseInt(myjson.resource[i].obj[j].id) == search){

                var newobj = myjson.resource[i].obj[j];

                var obj_action = newobj.action;
                for(var k = 0 ; k < obj_action.length ; k++){

                    if(obj_action[k].name == "ON"){
                        newobj.action = obj_action[k];
                    }
                }
            }   
        }
    }

    console.log(myjson.resource[1].obj[0]);

</script>

I can easily save in the variable newobj the object that i want. But why the initial json is changed ???

UPDATE

ok, I understand that it is clearly problem of how I save the obj in newobj variable. i dont' save the object, but only the reference at the object in json . how can I save it in the variable newobj ? (not by reference?)

WhiteLine
  • 1,919
  • 2
  • 25
  • 53
  • 2
    Tip: Don't ever use `eval()`. If you need it, it means there's something wrong with your code. – emerson.marini Sep 29 '14 at 14:56
  • I know, but I just reported the problem in a simplified manner – WhiteLine Sep 29 '14 at 14:57
  • 1
    [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) and your link to the "json" points to an empty site. – epascarello Sep 29 '14 at 15:00
  • 1
    Arrays are copied by reference, you do not make a "copy" of it. – epascarello Sep 29 '14 at 15:03
  • 1
    Correct, that is not a copy, you are referencing it... – epascarello Sep 29 '14 at 15:04
  • 1
    FWIW, your problem has nothing to do with JSON but with how objects work in JavaScript. – Felix Kling Sep 29 '14 at 15:05
  • Maybe you find this useful: http://stackoverflow.com/a/18843630/724991 – Uxío Sep 29 '14 at 15:06
  • all those deeply nested iterations are crying out for `.forEach` calls – Alnitak Sep 29 '14 at 15:06
  • ok, I understand that it is clearly problem of how I save the obj in newobj variable. i dont' save the object, but only the reference at the object in json – WhiteLine Sep 29 '14 at 15:13
  • Saying something like *"the reference at the object in json"* doesn't make sense. JSON is a **textual** data-exchange format. You can *serialize* a JavaScript object *to* JSON with `var json = JSON.stringify(obj);` or you can *deserialize* an object *from* JSON with `var obj = JSON.parse(json);`. However, once you did this you are just working with (JavaScript) objects, not JSON. – Felix Kling Sep 29 '14 at 15:36

1 Answers1

1

One way of decoupling object references is to serialize and deserialize as JSON. In this case, you could do something like this:

var newobj = JSON.parse(JSON.stringify(myjson.resource[i].obj[j]));

Note that this won't work every time. Complex objects with functions or prototypes won't be saved, for example.

It also comes with a slight performance hit.

Scimonster
  • 32,893
  • 9
  • 77
  • 89