I have a global array in javascript say
jsonArr = ["location","department","grade"];
now inside my method i am doing this
var newArr = [];
newArr = jsonArr;
var sorted_arr = newArr.sort();
my newArr is getting sorted but problem is along with jsonArr also got sorted i dont want to sort jsonArr
what is the problem can anyone plz help me ?
newArrandjsonArrare referencing the same array in your code (that’s what happens when you simply assign one array to another, like you’re doing with this statement:newArr = jsonArr;). You need to copy the array first; you can use the slice method for that: