最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - group object as 2d array by key - Stack Overflow

programmeradmin1浏览0评论

I have an array of objects. These objects have a property called time. I want to group these objects to the same array if their time are same.

{ "00:00" :
   [{"id":1, time: "00:05",
    {"id":1, time: "00:15",
    {"id":1, time: "00:20",
    {"id":2, time: "00:05",
    {"id":3, time: "00:05",
    {"id":4, time: "00:35 }]
}

I want to format the above data as:

{ "00:00" :[
    [{"id":1, time: "00:05"}, {"id":2, time: "00:05"}, {"id":3, time: "00:05"}],
    [{"id":1, time: "00:15" }],
    [{"id":1, time: "00:20" }],      
    [{"id":4, time: "00:35" }]]
}

Any advice on how can I acplish this?

I have an array of objects. These objects have a property called time. I want to group these objects to the same array if their time are same.

{ "00:00" :
   [{"id":1, time: "00:05",
    {"id":1, time: "00:15",
    {"id":1, time: "00:20",
    {"id":2, time: "00:05",
    {"id":3, time: "00:05",
    {"id":4, time: "00:35 }]
}

I want to format the above data as:

{ "00:00" :[
    [{"id":1, time: "00:05"}, {"id":2, time: "00:05"}, {"id":3, time: "00:05"}],
    [{"id":1, time: "00:15" }],
    [{"id":1, time: "00:20" }],      
    [{"id":4, time: "00:35" }]]
}

Any advice on how can I acplish this?

Share Improve this question edited Apr 28, 2018 at 4:43 Mamun 69k9 gold badges51 silver badges62 bronze badges asked Apr 28, 2018 at 4:08 EkoarEkoar 5011 gold badge7 silver badges20 bronze badges 2
  • Are there alot of properties like "00:00" or just one? – Eddie Commented Apr 28, 2018 at 4:10
  • there can be "01:00" , "02:00" ...etc. – Ekoar Commented Apr 28, 2018 at 4:13
Add a ment  | 

3 Answers 3

Reset to default 4

You can use reduce to summarize your object and use Object.values to convert the object into array

let obj={"00:00" :[{"id":1,time:"00:05"},{"id":1,time:"00:15"},{"id":1,time:"00:20"},{"id":2,time:"00:05"},{"id":3,time:"00:05"},{"id":4,time:"00:35"}],"02:00" :[{"id":1,time:"00:05"},{"id":1,time:"00:15"},{"id":1,time:"00:20"},{"id":2,time:"00:05"},{"id":3,time:"00:05"},{"id":4,time:"00:35"}]}

var result = Object.entries(obj).reduce((c, v) => {
  c[v[0]] = Object.values(v[1].reduce((a, o) => {
    a[o.time] = a[o.time] || [];
    a[o.time].push(o);
    return a;
  }, {}));
  return c;
}, {});


console.log(result);

Use .reduce to put all matching objects into an array inside an object indexed by the time, and then extract the values from the object:

const input = { "00:00" :
   [{"id":1, time: "00:05"},
    {"id":1, time: "00:15"},
    {"id":1, time: "00:20"},
    {"id":2, time: "00:05"},
    {"id":3, time: "00:05"},
    {"id":4, time: "00:35" }]
};
const outputObj = input['00:00'].reduce((accum, { id, time }) => {
  if (!accum[time]) accum[time] = [];
  accum[time].push({ id, time });
  return accum;
}, {});
const outputArr = Object.values(outputObj);
console.log(outputArr);

You can also extend it to apply to multiple properties of the input array:

const input = { "00:00" :
   [{"id":1, time: "00:05"},
    {"id":1, time: "00:15"},
    {"id":1, time: "00:20"},
    {"id":2, time: "00:05"},
    {"id":3, time: "00:05"},
    {"id":4, time: "00:35" }],
    "01:00" :
   [{"id":1, time: "01:05"},
    {"id":1, time: "01:15"},
    {"id":1, time: "01:20"},
    {"id":2, time: "01:05"},
    {"id":3, time: "01:05"},
    {"id":4, time: "01:35" }],
};
function transformInputArrToGroupedArr(inputArr) {
  const outputObj = inputArr.reduce((accum, { id, time }) => {
    if (!accum[time]) accum[time] = [];
    accum[time].push({ id, time });
    return accum;
  }, {});
  return Object.values(outputObj);
}
const transformedInput = Object.entries(input).reduce((accum, [ key, arr ]) => {
  accum[key] = transformInputArrToGroupedArr(arr);
  return accum;
}, {});
console.log(transformedInput);

You can try with Array's reduce() like the following way:

var obj = { "00:00" :
   [{"id":1, time: "00:05"},
    {"id":1, time: "00:15"},
    {"id":1, time: "00:20"},
    {"id":2, time: "00:05"},
    {"id":3, time: "00:05"},
    {"id":4, time: "00:35" }]
}

var res = obj['00:00'].reduce(function(a, c){
  a[c.time] = a[c.time] || [];
  a[c.time].push(c);
  return a;
}, {});

console.log(res);
.as-console-wrapper { top: 0; }

发布评论

评论列表(0)

  1. 暂无评论