如何对数组进行分组并根据每个组将单个属性返回给数组?

时间:2018-07-09 09:52:56

标签: javascript lodash

我有一个对象数据数组,我需要根据日期对每个对象进行分组,一旦将其分组,我就需要返回一组已分组的数组。

请参阅以下数据

//input
var arr = [{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample1" },
{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample2" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample3" },
{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample4" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample5" },
{ id: 5, date: "2018-07-08T07:00:00Z", data: "Sample6" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample7" },
{ id: 5, date: "2018-10-08T07:00:00Z", data: "Sample8" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample9" },
{ id: 5, date: "2018-07-05T07:00:00Z", data: "Sample10" }]


//expected grouping
var arr1 = [{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample1" },
{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample2" },
{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample4" }]

var arr2 = [{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample3" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample5" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample7" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample9" }]

var arr3 = [{ id: 5, date: "2018-07-05T07:00:00Z", data: "Sample10" }]

var arr4 = [{ id: 5, date: "2018-10-08T07:00:00Z", data: "Sample6" },
{ id: 5, date: "2018-10-08T07:00:00Z", data: "Sample8" }]

//expected output

var array1 = ["Sample1", "Sample2", "Sample4"]
var array2 = ["Sample3", "Sample5", "Sample7", "Sample9"]
var array3 = ["Sample10"]
var array4 = ["Sample6", "Sample8"]


 var groupedData = _.groupBy(arr, "date");
 console.log(groupedData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

使用lodash我已经取得了很多成就

var groupedData = _.groupBy(arr, "date");
var keys = _.keys(groupedData)

console.log(groupedData)

将其工作分组。我如何链接方法并返回块数组

3 个答案:

答案 0 :(得分:1)

您可以像这样简化代码。

var arr = [{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample1" },
{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample2" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample3" },
{ id: 5, date: "2018-09-29T07:00:00Z", data: "Sample4" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample5" },
{ id: 5, date: "2018-07-08T07:00:00Z", data: "Sample6" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample7" },
{ id: 5, date: "2018-10-08T07:00:00Z", data: "Sample8" },
{ id: 5, date: "2018-08-18T07:00:00Z", data: "Sample9" },
{ id: 5, date: "2018-07-05T07:00:00Z", data: "Sample10" }]

const obj = {};
arr.forEach(a => {
  if (obj[a.date]) {
    obj[a.date].push(a.data);
  } else {
    obj[a.date] = [a.data];
  }
});
console.log(obj);

答案 1 :(得分:0)

也许有一种更优雅的方法可以实现这一点,但是我将创建新的数组并使用类似于此处描述的方法:Group array items using object

答案 2 :(得分:0)

function group(arr){

    var dates = [];
    var output = [];

    for(var i=0; i<arr.length; i+=1){
        var date = arr[i].date;
        if(!dates.includes(date)) dates.push(date);
    }

    for(var i=0; i<dates.length; i+=1){
        var date = dates[i];
        var tempArr = [];

        for(var j=0; j<arr.length; j+=1)    
            if(date == arr[j].date) tempArr.push(arr[j].data);

        output.push(tempArr);
    }

    return output;
}