官术网_书友最值得收藏!

Grouping the loaded data so we only have the top 10 names for both sexes

At this point, we've only loaded the data. If you look back at the figure, you can see that we create a chart using the top 10 female and male names. With the following lines of code, we convert the big incoming data array to an array that contains just the top 10 female and male names:

var grouped = _.groupBy(data, 'sex'); 
var top10F = grouped['F'].slice(0, namesToShow);
var top10M = grouped['M'].slice(0, namesToShow);

var both = top10F.concat(top10M.reverse());

Here we use the lodash's groupBy function,to sort our data based on the sex property of each row. Next we take the first 10 (namesToShow) elements from the grouped data, and create a single array from them using the concat function. We also reverse the top10M array to make the highest boy's name appear at the bottom of the chart (as you can see when you look at the example).

主站蜘蛛池模板: 徐闻县| 南陵县| 刚察县| 花莲县| 南平市| 应城市| 宜川县| 崇左市| 墨脱县| 张家界市| 禹州市| 凤凰县| 宁德市| 光泽县| 芒康县| 祁阳县| 吉水县| 河北省| 凯里市| 南城县| 卓资县| 丹寨县| 鹤壁市| 武乡县| 调兵山市| 洛川县| 阳信县| 大理市| 瑞金市| 马关县| 南京市| 昌邑市| 会泽县| 布尔津县| 沭阳县| 色达县| 思南县| 阿巴嘎旗| 翁源县| 响水县| 婺源县|