能否简化功能的编写?

时间:2019-06-26 21:08:20

标签: javascript

我有以下输入数组。

[0: {from: "2019-05-15T15:00:00-05:00", to: "2019-05-15T15:30:00-05:00", group: 0}
1: {from: "2019-05-15T15:30:00-05:00", to: "2019-05-15T16:00:00-05:00", group: 0}
2: {from: "2019-05-15T16:00:00-05:00", to: "2019-05-15T16:30:00-05:00", group: 0}
3: {from: "2019-05-15T16:30:00-05:00", to: "2019-05-15T17:00:00-05:00", group: 0}
4: {from: "2019-05-15T18:00:00-05:00", to: "2019-05-15T18:30:00-05:00", group: 1}
5: {from: "2019-05-15T18:30:00-05:00", to: "2019-05-15T19:00:00-05:00", group: 1}
6: {from: "2019-05-15T19:00:00-05:00", to: "2019-05-15T19:30:00-05:00", group: 1}]

我希望输出是根据组值划分的子数组的集合。预期的输出如下

[[0: {from: "2019-05-15T15:00:00-05:00", to: "2019-05-15T15:30:00-05:00", group: 0}
1: {from: "2019-05-15T15:30:00-05:00", to: "2019-05-15T16:00:00-05:00", group: 0}
2: {from: "2019-05-15T16:00:00-05:00", to: "2019-05-15T16:30:00-05:00", group: 0}
3: {from: "2019-05-15T16:30:00-05:00", to: "2019-05-15T17:00:00-05:00", group: 0}]
,
[0: {from: "2019-05-15T18:00:00-05:00", to: "2019-05-15T18:30:00-05:00", group: 1}
1: {from: "2019-05-15T18:30:00-05:00", to: "2019-05-15T19:00:00-05:00", group: 1}
2: {from: "2019-05-15T19:00:00-05:00", to: "2019-05-15T19:30:00-05:00", group: 1}]]

下面是我要完成任务的代码。

let rawData = '[{"from":"2019-05-15T15:00:00-05:00","to":"2019-05-15T15:30:00-05:00","group":0},{"from":"2019-05-15T15:30:00-05:00","to":"2019-05-15T16:00:00-05:00","group":0},{"from":"2019-05-15T16:00:00-05:00","to":"2019-05-15T16:30:00-05:00","group":0},{"from":"2019-05-15T16:30:00-05:00","to":"2019-05-15T17:00:00-05:00","group":0},{"from":"2019-05-15T18:00:00-05:00","to":"2019-05-15T18:30:00-05:00","group":1},{"from":"2019-05-15T18:30:00-05:00","to":"2019-05-15T19:00:00-05:00","group":1},{"from":"2019-05-15T19:00:00-05:00","to":"2019-05-15T19:30:00-05:00","group":1}]';
var data = JSON.parse(rawData);
var input = data.map(x => [x]);
var output = [];
output.push(input.reduce(function(acc, cur, idx, src) {
  if (acc[0].group == cur[0].group) {
    acc.push(cur[0]);
    return acc;
  } else {
    output.push(acc);
    return cur;
  }
}));
console.log(output);

有没有一种方法可以使用map / reduce / filter / etc等功能性概念以更有效的方式编写代码??

2 个答案:

答案 0 :(得分:0)

您可以使用group属性生成一个对象,该对象包含按group value分组的元素,并最终获得Object.values()

let rawData = '[{"from":"2019-05-15T15:00:00-05:00","to":"2019-05-15T15:30:00-05:00","group":0},{"from":"2019-05-15T15:30:00-05:00","to":"2019-05-15T16:00:00-05:00","group":0},{"from":"2019-05-15T16:00:00-05:00","to":"2019-05-15T16:30:00-05:00","group":0},{"from":"2019-05-15T16:30:00-05:00","to":"2019-05-15T17:00:00-05:00","group":0},{"from":"2019-05-15T18:00:00-05:00","to":"2019-05-15T18:30:00-05:00","group":1},{"from":"2019-05-15T18:30:00-05:00","to":"2019-05-15T19:00:00-05:00","group":1},{"from":"2019-05-15T19:00:00-05:00","to":"2019-05-15T19:30:00-05:00","group":1}]';
var data = JSON.parse(rawData);

var res = data.reduce(function(acc, cur)
{
    acc[cur.group] = acc[cur.group] || [];
    acc[cur.group].push(cur);
    return acc;
}, {});

console.log(Object.values(res));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

但是,在特定情况下,组值来自[0 to N],您可以缩短代码的时间。更重要的是,具有group属性等于i的元素将位于最终数组的索引i上:

let rawData = '[{"from":"2019-05-15T15:00:00-05:00","to":"2019-05-15T15:30:00-05:00","group":0},{"from":"2019-05-15T15:30:00-05:00","to":"2019-05-15T16:00:00-05:00","group":0},{"from":"2019-05-15T16:00:00-05:00","to":"2019-05-15T16:30:00-05:00","group":0},{"from":"2019-05-15T16:30:00-05:00","to":"2019-05-15T17:00:00-05:00","group":0},{"from":"2019-05-15T18:00:00-05:00","to":"2019-05-15T18:30:00-05:00","group":1},{"from":"2019-05-15T18:30:00-05:00","to":"2019-05-15T19:00:00-05:00","group":1},{"from":"2019-05-15T19:00:00-05:00","to":"2019-05-15T19:30:00-05:00","group":1}]';
var data = JSON.parse(rawData);

var res = data.reduce(function(acc, cur)
{
    acc[cur.group] = acc[cur.group] || [];
    acc[cur.group].push(cur);
    return acc;
}, []);

console.log(res);
console.log("group 0 =>", res[0]);
console.log("group 1 =>", res[1]);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

答案 1 :(得分:0)

如果您乐于使用标准Array类之外的其他库,则可以使用lodash之类的东西。

   my_list = album_length['release_year'].apply(str).tolist()

   f, (ax1, ax2) = plt.subplots(1, 2, sharex= False, sharey=True, figsize= 
   (20, 15))
   ax1 = sns.barplot(x= album_length['minutes'],
        y=album_length['album'],orient = 'h')
   ax2.table(cellText=my_list, loc='center')

以下与已接受的答案相同,但在ES6中

import { groupBy } from 'lodash'
const res = groupBy(data, 'group')
//convert to nested arrays
Object.values(res).map(e => e)