-1

oldList:

var oldList = [
  {id:1, time:'2018-02-06 09:00-10:00', title:'aa'},
  {id:2, time:'2018-02-06 11:00-12:00', title:'bb'},
  {id:3, time:'2018-02-07 10:00:02', title:'cc'},
  {id:4, time:'2018-02-07 09:00-10:00', title:'dd'} 
];
console.log(oldList);

Desired:

var newList = [
  {
    '2018-02-06' : [
      {id:1, time:'2018-02-06 09:00-10:00', title:'aa'},
      {id:2, time:'2018-02-06 11:00-12:00', title:'bb'},
    ]
  },
  {
    '2018-02-07' : [
      {id:4, time:'2018-02-07 09:00-10:00', title:'dd'},
      {id:3, time:'2018-02-07 10:00:02', title:'cc'},
    ]
  },  
];
console.log(newList);

How can I get the following result from this array and object? I haven't found a good solution at the moment。

XiaoHu
  • 57
  • 1
  • 8
  • Why do you want an array of objects with an arbitrary key? better `[{ date: "...", items: [...]}]` or a single map of items by date `var itemsByDate = {'2018-02-06': [...], '2018-02-07': [...]}` – Thomas Feb 07 '18 at 09:33
  • check this out https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key – dfsq Feb 07 '18 at 09:33
  • Solutions are not just found, you've to make some work to create an algorithm for the task. Please show what you've tried. – Teemu Feb 07 '18 at 09:36
  • `if (using lodash) _.groupBy else use lodash` ;) – georg Feb 07 '18 at 09:39

4 Answers4

2

You can use reduce for this.

var oldList = [{
    id: 1,
    time: '2018-02-06 09:00-10:00',
    title: 'aa'
  },
  {
    id: 2,
    time: '2018-02-06 11:00-12:00',
    title: 'bb'
  },
  {
    id: 3,
    time: '2018-02-07 10:00:02',
    title: 'cc'
  },
  {
    id: 4,
    time: '2018-02-07 09:00-10:00',
    title: 'dd'
  }
];

var newList = oldList.reduce(function(c, i) {
  let t = i.time.split(" ")[0];
  c[t] = c[t] || [];
  c[t].push(i);
  return c;
}, {});

console.log( newList );

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Eddie
  • 26,593
  • 6
  • 36
  • 58
1
var newList = {};
for (var i = 0; i < oldList.length; i++) {
  var item = oldList[i];
  var key = item.time; //here you can transform the key as you like (ie remove time)
  if (newList[key] == null) {
    newList[key] = [];
  }
  newList[key].push(item );
}

I created a dictionary. For each item in your old list I check if in the new list exist a key with your timestamp. If not, create a new entry with an empty array. Then, in both case, push your item into the specific array

nemo
  • 1,675
  • 10
  • 16
1

you can use lodash to do this.

var newList = _.groupBy(oldList, function(o) {
    return o.time.split(" ")[0];
});
Cr.
  • 886
  • 7
  • 12
0

Here's a solution without using reduce.

var oldList = [{
  id: 1,
  time: '2018-02-06 09:00-10:00',
  title: 'aa'
}, {
  id: 2,
  time: '2018-02-06 11:00-12:00',
  title: 'bb'
}, {
  id: 3,
  time: '2018-02-07 10:00:02',
  title: 'cc'
}, {
  id: 4,
  time: '2018-02-07 09:00-10:00',
  title: 'dd'
}];

var uniqueDates = []

for (i in oldList) {
  if (uniqueDates.indexOf(oldList[i]['time'].split(' ')[0]) == -1) {
    uniqueDates.push(oldList[i]['time'].split(' ')[0])
  }
}

var newList = []
for (i in uniqueDates) {
  var val = {}
  val[uniqueDates[i]] = []
  for (j in oldList) {
  if(oldList[j]['time'].split(' ')[0] == uniqueDates[i]){
     val[uniqueDates[i]].push(oldList[j])
    }
  }
  newList.push(val)
}

console.log(newList)

But I like @Eddie's answer better

Aditya
  • 1,357
  • 1
  • 9
  • 19