I was wondering how can I map data of 2 arrays such that I can use the data to generate charts using d3.js
Current I have 2 arrays:
wordsArray = {"THE","MAIN","PURPOSE","OF","PHOTOSYNTHESIS","IS","TO","MAKE",
"FOOD","FOR","PLANT","IT","PRODUCE","SUGAR","MANUFACTURE"};
computationArray = {6,1,2,2,1,3,3,1,3,4,4,2,1,2,1};
The wordsArray contains all the distinct words and the computationArray contains the number of times each word appears.
I would like to know if there is any shortcut to make it look like this:
var data = [{"words":"THE", "count": "6"},
{"words":"MAIN", "count": "1"}, {"words":"PURPOSE", "count": "2"},
{"words":"OF", "count": "2"}.... ];
Thanks!
What you're looking for is the zip function in d3:
d3.zip(wordsArray, computationArray).map(function(e) {
return { words: e[0], count: e[1] };
});
shortcut is called underscore.js
npm install underscore
using that fine lib you can do:
var data = _.map(_.zip(wordsArray, computationArray), function(v){
return {words: v[0], count: v[1]};
});