Consider the following:
var _processSupplementalData = function(supfData) {
if (!supfData.length > 0) {
return;
}
var sectionData = {
label: supfData[0].label,
supplementalLinks: [],
}
supfData.forEach(function(data, index, object) {
sectionData.supplementalLinks.push(data);
supfData.splice(index, 1);
});
//dataObservableArray.push(sectionData);
console.log(sectionData);
}
my question is simple:
supfData
is an array of 4 objects. With the code as it stands, the sectionData
object, at the end of this, has a supplementalLinks
array of size 2.
How ever if I remove the splice
code, the supplementalLinks
arry now has the proper size: 4.
How am I using splice wrong such that I only have 2 objects in the array instead of 4?
shift
does the same thing for me.
What do I expect to happen?
Every an item is added to the array it needs to be removed from the array I am iterating over. If there are 4 objects in the array I am iterating over then here should be 4 objects in the sectionData.supplementalLinks
array.