I have an issue I cant get to the bottom of, so any help would be much appreciated.
- A the top of my scipt I declare a global value ('
_raw
') - (Using jQuery) I make an Ajax call, which returns JSON array data (I have checked, the JSON data is correct)
- I take this JSON response and assign it to
_raw
- When I click a link,
_raw
gets passed to a function, e.g.function myFunction(dataArray)
, called withmyFunction(_raw)
- Within this function, based on some
criteria,
dataArray
is spliced (i.e.dataArray.splice(2,1)
) dataArray
is then returned.
e.g.
var _raw;
// AJAX call sets RAW to an array e.g. 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 this is run, returns 'Pear', however, second time, returns 'Coconut'- as if the splice is being performed on _raw as well as myArray/data...
});
- I appreciate there is some poor code above, this is more to illustrate the issue
The problem I have, is that as far as I can see, the only time _raw
is set, is during the AJAX call, however, when the function myFunction is called, passing _raw
, the splice also seems to effect _raw
itself. Why is this happening?