-5
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 日}

这就是我能够获得用于分组目的的整体级别的计数。

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

请访问: https ://jsfiddle.net/Saily/6nzxd6ss/

4

1 回答 1

1

我也喜欢使用减少。在很多情况下都非常方便。

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

于 2016-06-01T14:08:52.333 回答