【问题标题】:The operation of the JavaScript array and objectJavaScript 数组和对象的操作
【发布时间】:2018-02-07 18:03:27
【问题描述】:

旧列表:

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

期望:

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

如何从这个数组和对象中得到以下结果? 暂时还没有找到好的解决办法。

【问题讨论】:

  • 为什么要一个带有任意键的对象数组?更好的[{ date: "...", items: [...]}] 或按日期排列的单个项目地图var itemsByDate = {'2018-02-06': [...], '2018-02-07': [...]}
  • 解决方案不只是找到,你必须做一些工作来为任务创建算法。请展示您的尝试。
  • if (using lodash) _.groupBy else use lodash ;)

标签: javascript arrays node.js object


【解决方案1】:

您可以为此使用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 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

【讨论】:

    【解决方案2】:

    您可以使用lodash 来执行此操作。

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

    【讨论】:

      【解决方案3】:
      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 );
      }
      

      我创建了一本字典。对于旧列表中的每个项目,我检查新列表中是否存在带有时间戳的键。如果没有,则使用空数组创建一个新条目。然后,在这两种情况下,将您的项目推送到特定的数组中

      【讨论】:

        【解决方案4】:

        这是一个不使用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)

        但我更喜欢@Eddie 的回答

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-02
          • 2021-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-28
          • 2014-02-13
          • 1970-01-01
          相关资源
          最近更新 更多