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

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).

主站蜘蛛池模板: 和田县| 邵阳县| 白水县| 奈曼旗| 洮南市| 白沙| 惠东县| 衢州市| 手游| 扎鲁特旗| 建水县| 灌云县| 榆中县| 兴山县| 景宁| 上饶县| 水城县| 忻州市| 永胜县| 枞阳县| 拜城县| 铜山县| 洛浦县| 崇礼县| 东平县| 公安县| 新宁县| 灵寿县| 岳西县| 饶河县| 芜湖市| 高陵县| 石屏县| 晋中市| 美姑县| 图片| 常熟市| 高尔夫| 大理市| 漳平市| 德化县|