Struggling with a baffling issue that has me stumped, so any assistance would be greatly appreciated.
- At the beginning of my script, I establish a global variable called '
_raw
' - (Using jQuery) I execute an Ajax request which returns JSON array data (I have verified that the JSON data is accurate)
- I take this JSON response and assign it to
_raw
- Upon clicking a link,
_raw
is passed to a function, for example,function myFunction(dataArray)
, invoked bymyFunction(_raw)
- Inside this function, based on specific criteria,
dataArray
gets spliced (such asdataArray.splice(2,1)
) - The modified
dataArray
is then returned.
For instance:
var _raw;
// AJAX call populates RAW with an array like Apple, Banana, Pear, Pineapple, Coconut, Raspberry
myFunction(dataArray){
var data=dataArray;
data.splice(2, 1);
return data[0];
}
$('a').click(function(){
result = myFunction(_raw);
alert(result);
// First time running this, 'Pear' is returned; however, second time, 'Coconut' is returned - as if the splice operation affects both _raw and myArray/data...
});
- I acknowledge that there are some shortcomings in the code above, but it serves to highlight the problem at hand.
The issue I am facing pertains to the fact that, to my understanding, the only instance where _raw
is altered is during the AJAX call. Yet, when calling the myFunction function and passing _raw
, the splice action also seems to impact _raw
itself. Why is this occurring?