var developers = [
{ name: "Joe", age: 23, overallLevel: "high", date: "Aug 14, 2015" },
{ name: "Sue", age: 28, overallLevel: "advanced", date: "Aug 11, 2015" },
{ name: "Jon", age: 32, overallLevel: "high", date: "Aug 10, 2015" },
{ name: "Bob", age: 24, overallLevel: "high", date: "Sept 07, 2015" },
{ name: "Johnson", age: 23, overallLevel: "advanced", date: "Aug 20, 2015" },
{ name: "Paul", age: 25, overallLevel: "basic", date: "Jan 30, 2016" },
{ name: "Jane", age: 27, overallLevel: "intermediate", date: "Aug 30, 2016" },
{ name: "Chris", age: 20, overallLevel: "basic", date: "Aug 1, 2016" },
{ name: "Susan", age: 25, overallLevel: "basic", date: "Dec 12, 2015" },
{ name: "Jenny", age: 23, overallLevel: "intermediate", date: "Aug 30, 2016" },
{ name: "Stone", age: 25, overallLevel: "basic", date: "June 13, 2016" },
]
以下是从数组中提取的数据,表示级别后跟日期。
"高" : 2015年8月10日, 2015年8月14日, 2015年9月7日
"高级" : 2015年8月11日, 2015年8月20日
"碱性" : 2016年1月30日, 2016年8月1日, 2015年12月12日, 2016年6月13日,
"中间" : 2016年8月30日, 2016年8月30日
预期输出:要求最小和最大日期范围,即进一步显示
{ 高:2015年8月10日 - 2015年9月7日, 高级:2015年8月11日 - 2015年8月20日, 基本:2015年12月12日 - 2016年8月1日, 中级:2016年1月30日 }
这是我能够获得用于分组目的的overallLevel计数。
var developers = [
{ name: "Joe", age: 23, overallLevel: "high", date: "Aug 14, 2015" },
{ name: "Sue", age: 28, overallLevel: "advanced", date: "Aug 11, 2015" },
{ name: "Jon", age: 32, overallLevel: "high", date: "Aug 10, 2015" },
{ name: "Bob", age: 24, overallLevel: "high", date: "Sept 07, 2015" },
{ name: "Johnson", age: 23, overallLevel: "advanced", date: "Aug 20, 2015" },
{ name: "Paul", age: 25, overallLevel: "basic", date: "Jan 30, 2016" },
{ name: "Jane", age: 27, overallLevel: "intermediate", date: "Aug 30, 2016" },
{ name: "Chris", age: 20, overallLevel: "basic", date: "Aug 1, 2016" },
{ name: "Susan", age: 25, overallLevel: "basic", date: "Dec 12, 2015" },
{ name: "Jenny", age: 23, overallLevel: "intermediate", date: "Aug 30, 2016" },
{ name: "Stone", age: 25, overallLevel: "basic", date: "June 13, 2016" },
],
overallLevel = developers.reduce(function (r, a) {
r[a.overallLevel] = (r[a.overallLevel] || 0) + 1;
return r;
}, {});
console.log(overallLevel);
答案 0 :(得分:0)
我也很喜欢使用reduce。它对很多案件来说非常方便。
var developers = [
{ name: "Joe", age: 23, overallLevel: "high", date: "Aug 14, 2015" },
{ name: "Sue", age: 28, overallLevel: "advanced", date: "Aug 11, 2015" },
{ name: "Jon", age: 32, overallLevel: "high", date: "Aug 10, 2015" },
{ name: "Bob", age: 24, overallLevel: "high", date: "Sept 07, 2015" },
{ name: "Johnson", age: 23, overallLevel: "advanced", date: "Aug 20, 2015" },
{ name: "Paul", age: 25, overallLevel: "basic", date: "Jan 30, 2016" },
{ name: "Jane", age: 27, overallLevel: "intermediate", date: "Aug 30, 2016" },
{ name: "Chris", age: 20, overallLevel: "basic", date: "Aug 1, 2016" },
{ name: "Susan", age: 25, overallLevel: "basic", date: "Dec 12, 2015" },
{ name: "Jenny", age: 23, overallLevel: "intermediate", date: "Aug 30, 2016" },
{ name: "Stone", age: 25, overallLevel: "basic", date: "June 13, 2016" },
],
reduced = developers.reduce((p,c) => {var d = new Date(c.date);
p[c.overallLevel] ? d < p[c.overallLevel][0] && d < p[c.overallLevel][1] ? p[c.overallLevel][0] = d
: p[c.overallLevel][1] = d
: p[c.overallLevel] = [d,d];
return p},{});
reduced = Object.keys(reduced).reduce((p,c) => (p[c] = p[c].map(e => e.toDateString(2)), p),reduced);
console.log(reduced);
&#13;