I have a nested array named $scope.instruments, its contents are:
collapsed:
expanded:
I also have an object:
This object contains 2 arrays: AttributeID and SPAttributeRefID.
Currently I am flattening the $scope.instruments array using reduce function:
$scope.instruments = $scope.instruments.reduce(function (result, instrument) {
result[instrument.ID] = instrument;
return result
}, {});
Then I access the AttributeID of the object and assign it to a variable like this:
$scope.recordInstrument = $scope.instruments[data.AttributeID[0]].ID;
I would rather use a different method to obtain the same result. I read that an array can be flatten using a for loop, that it is a more efficient method of doing it. Unfortunately, what I have tried so far, is not giving me the same results.
var arrInstruments = $scope.instruments;
var arrLength = arrInstruments.length;
for (var i = 0; i < arrLength; i++) {
console.log(arrInstruments[i]);
}
Can someone give me a hand converting the code that uses the reduce function to use a loop and have the same result in the assignment of AttributeID?
Many thanks.



reduceis not a library and not inefficient either. Why not just use your working solution?reducecode isn't even flattening anything either, rather it is building an ID lookup table).