Saturday 16 February 2019

JavaScript array assign issue

When you do something like array2 = array1; you are just setting array2 as a reference to array1. To make a copy of array1 you need to do array2 = array1.slice();


Furthermore, you can not set Array elements with array1.value1 ='1';. What you have done there is convert your array to an Object. So what you really should be doing is:


var array1 = [];
var array2 = [];
array1[0] = 1;
array2 = array1.slice();
array2[1] = 2;

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...