按日期对数组中的项目进行分组

时间:2019-10-16 15:37:17

标签: javascript node.js

我有一个包含日期和bin颜色的数组。在同一日期收集了一些垃圾箱,因此我想对这些日期进行分组,然后使用一系列垃圾箱颜色。

我想要实现的数据结构是:

"items": [
        "item": {
            "date": "2019-10-18T00:00:00.000Z",
            "bins": [{
                "bin": "Blue"
            }, {
                "bin": "Grey"
            }]
        },
        ...
]

数据数组:

var dateList = [{
        "date": "2019-10-18T00:00:00.000Z",
        "bin": "Blue"
    }, {
        "date": "2019-11-08T00:00:00.000Z",
        "bin": "Blue"
    }, {
        "date": "2019-11-01T00:00:00.000Z",
        "bin": "Green"
    }, {
        "date": "2019-11-22T00:00:00.000Z",
        "bin": "Green"
    }, {
        "date": "2019-10-18T00:00:00.000Z",
        "bin": "Grey"
    }, {
        "date": "2019-11-01T00:00:00.000Z",
        "bin": "Grey"
    }]

这是我到目前为止的代码,它已经存在了,但是没有创建items.item,而是创建了items [date],但是我不知道最后一部分:

    var items = {};
    for(var i = 0; i < dateList.length; i++) {

        if(!items[dateList[i].date]) {

            items[dateList[i].date] = {};
            items[dateList[i].date].date = dateList[i].date;
            items[dateList[i].date].bins = [];
            items[dateList[i].date].bins.push( { "bin": dateList[i].bin });

        } else {
            items[dateList[i].date].bins.push( { "bin": dateList[i].bin }); 
        }
    } 

这将导致项目[日期]

"items": {
    "2019-10-18T00:00:00.000Z": {
        "date": "2019-10-18T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }, {
            "bin": "Grey"
        }]
    },
    "2019-11-08T00:00:00.000Z": {
        "date": "2019-11-08T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }]
    },
    "2019-11-01T00:00:00.000Z": {
        "date": "2019-11-01T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }, {
            "bin": "Grey"
        }]
    },
    "2019-11-22T00:00:00.000Z": {
        "date": "2019-11-22T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }]
    }
}

如果我能像item.item那样构造它,我会更喜欢

"items": [
    "item": {
        "date": "2019-10-18T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }, {
            "bin": "Grey"
        }]
    },
    "item": {
        "date": "2019-11-08T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }]
    },
    "item": {
        "date": "2019-11-01T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }, {
            "bin": "Grey"
        }]
    },
    "item": {
        "date": "2019-11-22T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }]
    }
]

谢谢

3 个答案:

答案 0 :(得分:1)

要获取对象数组,可以将其用作以下代码。请注意,不能保证使用Object.keys()时的顺序。

我放置了两种方法,一种是简单数组,第二种是使用item关键字作为数组中每个对象的包装器的数组。

const items = {
    "2019-10-18T00:00:00.000Z": {
        "date": "2019-10-18T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }, {
            "bin": "Grey"
        }]
    },
    "2019-11-08T00:00:00.000Z": {
        "date": "2019-11-08T00:00:00.000Z",
        "bins": [{
            "bin": "Blue"
        }]
    },
    "2019-11-01T00:00:00.000Z": {
        "date": "2019-11-01T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }, {
            "bin": "Grey"
        }]
    },
    "2019-11-22T00:00:00.000Z": {
        "date": "2019-11-22T00:00:00.000Z",
        "bins": [{
            "bin": "Green"
        }]
    }
}

const array = Object.keys(items).map(key => items[key]);
const arrayWithItems = Object.keys(items).map(key => ({item: items[key]}));
console.log(array);
console.log('!!!! -------- !!!!!');
console.log(arrayWithItems);

答案 1 :(得分:1)

我对此:

var dateList = [{
    "date": "2019-10-18T00:00:00.000Z",
    "bin": "Blue"
}, {
    "date": "2019-11-08T00:00:00.000Z",
    "bin": "Blue"
}, {
    "date": "2019-11-01T00:00:00.000Z",
    "bin": "Green"
}, {
    "date": "2019-11-22T00:00:00.000Z",
    "bin": "Green"
}, {
    "date": "2019-10-18T00:00:00.000Z",
    "bin": "Grey"
}, {
    "date": "2019-11-01T00:00:00.000Z",
    "bin": "Grey"
}]

let dictionary = {};
for(let i = 0; i < dateList.length; i++) {
  const item = dateList[i];
  if (!dictionary[item.date]) dictionary[item.date] = { date: item.date, bins: [] };
  dictionary[item.date].bins.push(item.bin);
}

const result = Object.values(dictionary);

// if you need to have distinct values in bins:
result.forEach(obj => obj.bins = new Array(...new Set(obj.bins)));

console.log(result);

答案 2 :(得分:0)

首先,您期望的结果无效。 javascript数组将不接受键值。数组是项目或对象的列表。以下是您可以获得的输出。

//Output
[
  {
    "date": "2019-10-18T00:00:00.000Z",
    "bins": [
      {
        "bin": "Blue"
      },
      {
        "bin": "Grey"
      }
    ]
  },
  {
    "date": "2019-11-08T00:00:00.000Z",
    "bins": [
      {
        "bin": "Blue"
      }
    ]
  },
  {
    "date": "2019-11-01T00:00:00.000Z",
    "bins": [
      {
        "bin": "Green"
      },
      {
        "bin": "Grey"
      }
    ]
  },
  {
    "date": "2019-11-22T00:00:00.000Z",
    "bins": [
      {
        "bin": "Green"
      }
    ]
  }
]

下面是导出输出的javascript代码。

//Code
var noDuplicates = [];
var lookupObject  = {};
for(var i in dateList) {
    lookupObject[dateList[i]['date']] = dateList[i];
}

//Create the array with unique dates
for(i in lookupObject) {
    noDuplicates.push(lookupObject[i].date);
}

var results=[]
//Iterate and filter the original array with same date value
for(i in noDuplicates) {
    var groupedArray = dateList.filter(obj => {
      return obj.date === noDuplicates[i]
    })
    //Form the bins array for the output
    var bins = groupedArray.map(obj => {
      return {
        'bin': obj.bin
      }
    })
    //Construct the output object
    results.push({
      'date': groupedArray[0].date,
      'bins': bins
    })
}

是的。这是一种直接解决方案,只需一点点操作即可首先获取唯一值。希望它能解决您的问题。

相关问题