我想知道如何映射2个数组的数据,以便我可以使用数据来生成使用d3.js的图表 目前我有2个阵列: wordsArray = {"THE","MAIN","PURPOSE","OF","PHOTOSYNTHESIS","IS","TO","MAKE", "FOOD","FOR","PLANT","IT","PRODUCE",
目前我有2个阵列:
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};
wordsArray包含所有不同的单词,calculateArray包含每个单词出现的次数.
我想知道是否有任何快捷方式使它看起来像这样:
var data = [{"words":"THE", "count": "6"}, {"words":"MAIN", "count": "1"}, {"words":"PURPOSE", "count": "2"}, {"words":"OF", "count": "2"}.... ];
谢谢!
您正在寻找的是d3中的 zip功能:d3.zip(wordsArray, computationArray).map(function(e) { return { words: e[0], count: e[1] }; });