- Expert Data Visualization
- Jos Dirksen
- 161字
- 2021-07-09 18:22:43
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).
推薦閱讀
- Android和PHP開發最佳實踐(第2版)
- 機器人Python青少年編程開發實例
- 編寫高質量代碼:改善C程序代碼的125個建議
- 人人都懂設計模式:從生活中領悟設計模式(Python實現)
- JavaScript入門經典
- Learning jQuery(Fourth Edition)
- C++20高級編程
- Building Serverless Web Applications
- Learning Android Application Testing
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)
- scikit-learn Cookbook(Second Edition)
- Java高并發編程詳解:深入理解并發核心庫
- 算法秘籍
- HTML5與CSS3權威指南
- 零基礎C語言學習筆記