1

This is my first function, i pass to json end if it is the first time i save the json is original.

 function loadchart2(div, json, lista,tipo) {
 var listaId = lista;
 if(l==0){
      jsonSecCall =JSON.parse(JSON.stringify(json));
      listaSecCall=listaId;
      l++;

}

I modify json ,and click a button. Call second function and call loadcart2 and pass the json original but actually receives the json modify, What???

$("#giorni").on('click',function () { var nuova=jsonSecCall;

$("#svgDateLine2").remove();
   loadchart2("content", nuova,listaSecCall,"giorni");
  checkContainer();


});

Probably the json it is gone for reference, is possible??

This is an example https://jsfiddle.net/tsjvmsd0/. First console log print json modified, is ok, the next console log they should print the original json, but print json modified.

Sax92
  • 11
  • 4
  • 3
    What the hell is this? ;-) JSON.parse(JSON.stringify(json)); – Rob Jul 18 '17 at 09:08
  • 2
    @Robert that's a deep copy of the object. – XCS Jul 18 '17 at 09:09
  • It's unclear what are you asking, can you provide the exact calls that are you making and what do you expect the parameters to be for each call? – XCS Jul 18 '17 at 09:11
  • I copy the json original. – Sax92 Jul 18 '17 at 09:12
  • I have to copy the json I receive at the first call of the function And pass it on to future calls, But I want to pass the first I receive not what is then changed – Sax92 Jul 18 '17 at 09:14
  • @Sax92 the function `loadchar2` is not show in the question completly. Can you add the rest of the code? – DomeTune Jul 18 '17 at 09:33
  • Loadchart2 is very long – Sax92 Jul 18 '17 at 09:37
  • @Sax92 What is `l`? Where is it defined ? Where do you get `jsonSecCall`? How does your `json` looks like ? What is `listaSecCall`? Why do you do `listaSecCall = listaId = lista`? – DomeTune Jul 18 '17 at 09:41
  • l is a variable = 0, so the copy is only made the first time – Sax92 Jul 18 '17 at 09:46
  • https://jsfiddle.net/tsjvmsd0/ this is example, when i click button in console the second json print is modified, not is the original – Sax92 Jul 18 '17 at 10:10
  • Everybody here (including myself) has been trying to answer your question when what we should have been doing is asking what result are you trying to achieve? It's not clear from your code. – Adam Jenkins Jul 18 '17 at 12:20

3 Answers3

0

This is the con of javascript. Everything in JS is passed by value, except objects, due to they are collections of references. That was done in performance reasons - it is costly to make copy of whole object.

To modify object without mutating the original, you should copy it. One way is to use Object.assign. It's probably better than making so deep copy of object ;-)

Then the snippet will be

var copy = {}
Object.assign(copy, source)
alexcleac
  • 29
  • 4
0

I dont know the types of your variables but normally I use Array.prototype.slice for copying.

var a = [1, 2, 3];
var b = a.slice(0); //Creates a new array

For copying objects see @alexcleac answer.

Code Spirit
  • 3,992
  • 4
  • 23
  • 34
  • Error: json.slice is not a function – Sax92 Jul 18 '17 at 09:29
  • @Sax92 cause its for array, as he pointed out. – DomeTune Jul 18 '17 at 09:31
  • @Sax92 - you have a clear misunderstanding of the difference between JSON - **a string** - and an object or array that can be represented in JSON format. – Adam Jenkins Jul 18 '17 at 09:31
  • My json is :edges : Array(4) nodes : Array(5) __proto__ : Object – Sax92 Jul 18 '17 at 09:35
  • @Sax92 i never saw such a json-string. – DomeTune Jul 18 '17 at 09:46
  • {"nodes":[{"txid":"0863d6798e830fecbc9e473b733980041afaf02cfa800f494323440a4b517149","total_address_input":"1","total_address_output":"2","time":"1300469394","block_height":"114042","total_value":"0.11","fee":0}], "edges":[{"source":"080f87a040eec22ebbd8d7224fc5ff38a8d2ee595acdbb6c3c782836403cbb61","target":"7dc8e6d15fc6da47b555e627898920a00e5a27a589fa9d7eda446d0ac69904d3"},]} – Sax92 Jul 18 '17 at 09:51
0

Firstly, it doesn't look like you are dealing with JSON, it looks like you are dealing with an object (or an array). JSON is a string.

Secondly, you aren't passing in the original JSON to loadchart2 (as you have figured out). You already modified the object reference by the variable jsonSecCall

What you probably want to do is actually maintain the original JSON (string) form of the object and use that as your original, unmodified object. You can do a JSON.parse on it whenever you want an object reference of your original.

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • jsonSecCall receive the original json, after json is modifies not jsonSecCall, i pass jsonSecCall in future calls but jsonSecCall It also contains the changes, jsonSecCall should contain the original. – Sax92 Jul 18 '17 at 09:33
  • You are making a copy of the object, but then you are later modifying that object and using it again. Create another variable that is just the result of JSON.stringify and then whenever you need it in loadChart2, call JSON.parse on the string that you saved to a variable – Adam Jenkins Jul 18 '17 at 09:38
  • yes is a copy, but the copy(jsonSecCall) Is modified if json changes and should not – Sax92 Jul 18 '17 at 09:39
  • this is an example, https://jsfiddle.net/tsjvmsd0/ , first console log is modified, is ok, the next console.log print json modified and not json original. – Sax92 Jul 18 '17 at 10:16