4

我想知道如何映射 2 个数组的数据,以便我可以使用数据生成使用 d3.js 的图表

当前我有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 包含所有不同的单词,而 computingArray 包含每个单词出现的次数。

我想知道是否有任何捷径可以使它看起来像这样:

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

谢谢!

4

2 回答 2

2

您正在寻找的是d3中的zip功能:

d3.zip(wordsArray, computationArray).map(function(e) {
    return { words: e[0], count: e[1] };
});
于 2012-12-28T03:36:10.717 回答
1

快捷方式称为underscore.js

npm 安装下划线

使用那个好的库你可以做:

var data = _.map(_.zip(wordsArray, computationArray), function(v){
   return {words: v[0], count: v[1]};
});
于 2012-12-28T03:31:44.403 回答