How to map data so that I can use it to create a pie chart?

I was wondering how can I match data from 2 arrays so that I can use the data to generate diagrams 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}; 

Array words contain all the different words, and computationArray indicates the number of times each word appears.

I would like to know if there is any shortcut so that it looks like this:

 var data = [{"words":"THE", "count": "6"}, {"words":"MAIN", "count": "1"}, {"words":"PURPOSE", "count": "2"}, {"words":"OF", "count": "2"}.... ]; 

Thanks!

+4
source share
2 answers

What you are looking for is zip in d3:

 d3.zip(wordsArray, computationArray).map(function(e) { return { words: e[0], count: e[1] }; }); 
+2
source

the shortcut is called underscore.js

npm set underline

using this beautiful lib library you can do:

 var data = _.map(_.zip(wordsArray, computationArray), function(v){ return {words: v[0], count: v[1]}; }); 
+1
source

All Articles