Skip to content

Instantly share code, notes, and snippets.

@MdaaaaO
Last active June 12, 2016 15:29
Show Gist options
  • Save MdaaaaO/1b26167d83276b8adc4248f0fd235a01 to your computer and use it in GitHub Desktop.
Save MdaaaaO/1b26167d83276b8adc4248f0fd235a01 to your computer and use it in GitHub Desktop.
Iteration over an object and manipulating every node in a loop by adding new calculated content.
getProject(filteredSAV, metricPROJECT, function (distribution, mapped) {
var keys = Object.keys(mapped);
var tasksLeft = keys.length;
function onComplete() {
saveJSON("./tmp/", "project.json", mapped);
}
keys.forEach(function (subchar) {
calcFrequencies(mapped[subchar], function (calculatedFrequencies) {
mapped[subchar].analysis.stats = calculatedFrequencies;
if (--tasksLeft === 0) {
onComplete();
}
});
});
});
@MdaaaaO
Copy link
Author

MdaaaaO commented Jun 7, 2016

calcFrequencies() takes a while.

Is there a better way to write such a loop?

@MdaaaaO
Copy link
Author

MdaaaaO commented Jun 12, 2016

is this actually the answer:

async.forEach(Object.keys(dataObj), function (item, callback){
console.log(item); // print the key
callback(); // tell async that the iterator has completed

}, function(err) {
console.log('iterating done');
});

??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment