I think your problem is caused by setting vocab to targetVocab. In JavaScript when you set a new variable to the value of an object variable then both variables will point to the same object:
var a = [{a:0}];
var b = a
a.splice(0,1);
console.log(b);//=[] empty array
What you're looking for is making a clone of your object, I could find goog.object.clone and goog.object.unsafeClone in the closure library.
clone will only do a shallow copy so if your array would contain immutable objects or primative types you can use clone but if you are going to do something with the cloned array as in the following example you'll need unsafeClone:
var a = [{a:0}];
var b = goog.object.clone(a);
a[0].a=22;
console.log(b[0]);//=22
// b isn't even an array here so I'm not sure what clone does
// but it doesn't do much
console.log(b instanceof Array);//false
If you're never going to change the values of the array's items and never call functions that'll change the items internal values (like copiedarray[0].changevalue();
or copiedarray[index].someval=newval;
) then you can copy a reference to the items in a new array:
var a = [{a:0}];
var b = a.concat([]);
// can't do a[0].a=newValue or it'll permanently change
a.splice(0,1);
console.log(b[0]);//=0
UnsafeClone will fully clone the object but you'll run the risk of starting a never ending loop when a property of any object (property) references itself:
var a = [{a:0}];
var b = goog.object.unsafeClone(a);
a[0].a=22;
console.log(b[0]);//=0
console.log(b instanceof Array);//true
In your case; since the object to be copied comes from a JSON string there won't be circular references but there may be a better way to do it since unsafeClone may be slower than goog.json.parse
...
if (request.isSuccess()) {
targetVocab = request.getResponseText();
setVocab();
}
...
var setVocab = function() {
vocab = goog.json.parse(targetVocab)['targetVocab'];
}
...