Here is one solution:
var isNewObject = function(newObject) {
return !currentArrayObjects.some(function(currentObject) {
return newObject.registration == currentObject.registration;
});
};
var onlyNewObjects = newDownloadedArray.filter(isNewObject);
We're basically saying "For every object in newDownloadedArray, look at every object in currentArrayObjects until you find one that has a matching registration. If you do, that object is included in onlyNewObjects. If you don't, it isn't.
Note that Array.prototype.filter and Array.prototype.some are only available in IE 9+, so you might want to use an equivalent helper method or utility library (like underscore) if you want to support older browsers.
This isn't very efficient. For arrays of 30 items, we're doing work a worst-case of 900 times (if the new array was completely unique, because it has to search through all 30 of the currentArrayObjects for each one of the newDownloadedArray).
But that's not really a lot in browser terms. And you can do a lot to speed it up. For example, instead of searching through the currentArrayObjects in the predicate, we could build an object with all the registrations:
// We want a set of registrations, but JavaScript doesn't
// have a native set class, so we're going to use the keys
// of an object to simulate sets, because object keys are
// basically sets of strings. Note that this won't work if
// registration isn't a string.
var currentRegistrations = {};
currentArrayObjects.forEach(function(currentObject) {
// AKA currentRegistrationSet.add(currentObject.registration) if we
// had an actual set class. I chose 'true' somewhat at random
// because it felt right; we'll never actually be accessing
// the value.
currentRegistrations[currentObject.registration] = true;
});
var isNewObject = function(newObject) {
// AKA !currentRegistrationSet.contains(newObject.registration) if we
// had an actual set class.
return !currentRegistrations.hasOwnProperty(newObject.registration);
}
var onlyNewObjects = newDownloadedArray.filter(isNewObject);
(Same caveat about Array.prototype.forEach)
Now we only have to do about 60 operations -- 30 to build the object ahead of time, and 30 more to check each one.
Your solution wasn't far off from the first one I posted. But you switched the for loops. It could be:
newDownloadedArray = JSON.parse(newDownloadedArray);
var onlyNewObjects = []
for (var i = 0; i < newDownloadedArray.length; i++) {
var isNewObject = true;
for (var j = 0; j < currentArrayObjects.length; j++) {
if (newDownloadedArray[i].registration == currentArrayObjects[j].registration) {
isNewObject = false;
break; // no reason to keep looking; we know it isn't new
}
}
if (isNewObject) {
onlyNewObjects.push(newDownloadedArray[i]);
}
}