I have an array of 5 objects that i need to loop through and return in an array of objects. However when i do that, it only returns the last object 5 times.
I've got two pieces of code doing pretty much the exact same thing one works where as another doesn't.
working code: (loops through a string, splits on | and returns an object in an array);
angular.forEach(exampleString, function(data) {
if (data.indexOf('|') !== -1) {
data = data.split('|');
//self.model.getDataFromArray()returns an object
newArray.push(new self.model.getDataFromArray(data));
}
});
//returns array of objects
doesn't work: (loops through objects and returns an object but only pushes the last 1 to the array 5 times)
angular.forEach(exampleObjects, function(data) {
this.push(new self.model.getDataFromObject(data));
}, newArray);
Is there anything that i'm doing or not doing that is causing this to fail? the only real difference i can tell is that i start with a string that i split as opposed to starting with an object.
just to clarify if i do the following i get the same repetition:
angular.forEach(exampleObjects, function(data) {
newArray.push(new self.model.getDataFromObject(data));
});