在嵌套数组中对对象进行分组

时间:2019-12-04 07:14:08

标签: javascript arrays object reduce

我在另一个数组中有2个数组。在这些数组中有对象,我想按对象键对它们进行分组。

父数组包含n个数组

[array_1, array_2]

就像我说的那样,在这些数组中还有一个充满对象的数组

数组1

taskTypeList: Array(7)
0: TaskType {Key: 313, Value: "R/I", IsDisabled: true, Duration: 1}
1: TaskType {Key: 312, Value: "MEL", IsDisabled: true, Duration: 2}
2: TaskType {Key: 311, Value: "SGH", IsDisabled: false, Duration: 9}
3: TaskType {Key: 309, Value: "LOC", IsDisabled: true, Duration: 4}
4: TaskType {Key: 485, Value: "TT", IsDisabled: true, Duration: 5}
5: TaskType {Key: 310, Value: "FOT", IsDisabled: true, Duration: 6}
6: TaskType {Key: 314, Value: "TS", IsDisabled: true, Duration: 7}

数组2

taskTypeList: Array(7)
0: TaskType {Key: 313, Value: "R/I", IsDisabled: true, Duration: 1}
1: TaskType {Key: 312, Value: "MEL", IsDisabled: true, Duration: 2}
2: TaskType {Key: 311, Value: "SGH", IsDisabled: true, Duration: 3}
3: TaskType {Key: 309, Value: "LOC", IsDisabled: false, Duration: 6}
4: TaskType {Key: 485, Value: "TT", IsDisabled: true, Duration: 5}
5: TaskType {Key: 310, Value: "FOT", IsDisabled: true, Duration: 6}
6: TaskType {Key: 314, Value: "TS", IsDisabled: true, Duration: 7}

我想按它们的键对这些数组进行分组。结果应该是这样的:

0:
309: [TaskType,TaskType]
310: [TaskType,TaskType]
311: [TaskType,TaskType]
312: [TaskType,TaskType]
313: [TaskType,TaskType]
314: [TaskType,TaskType]
485: [TaskType,TaskType]

该怎么办?

3 个答案:

答案 0 :(得分:2)

您可以使用spread运算符来连接数组,然后使用reduce方法按键进行分组

let array1 = [
	{Key: 313, Value: "R/I", IsDisabled: true, Duration: 1},
	{Key: 312, Value: "MEL", IsDisabled: true, Duration: 2},
	{Key: 311, Value: "SGH", IsDisabled: false, Duration: 9},
	{Key: 309, Value: "LOC", IsDisabled: true, Duration: 4},
	{Key: 485, Value: "TT", IsDisabled: true, Duration: 5},
	{Key: 310, Value: "FOT", IsDisabled: true, Duration: 6},
	{Key: 314, Value: "TS", IsDisabled: true, Duration: 7}
]

let array2 = [
	{Key: 313, Value: "R/I", IsDisabled: true, Duration: 1},
	{Key: 312, Value: "MEL", IsDisabled: true, Duration: 2},
	{Key: 311, Value: "SGH", IsDisabled: true, Duration: 3},
	{Key: 309, Value: "LOC", IsDisabled: false, Duration: 6},
	{Key: 485, Value: "TT", IsDisabled: true, Duration: 5},
	{Key: 310, Value: "FOT", IsDisabled: true, Duration: 6},
	{Key: 314, Value: "TS", IsDisabled: true, Duration: 7}
];

let container = [...array1, ...array2];
let result = container.reduce((acc, c) => ((acc[c.Key] = (acc[c.Key] || [])).push(c), acc),{});
console.log(result);

答案 1 :(得分:1)

一种方法是先将数组合并在一起,然后按每个Key值将其合并:

var parentArray = [
  [{
      Key: 313,
      Value: "R/I",
      IsDisabled: true,
      Duration: 1
    },
    {
      Key: 312,
      Value: "MEL",
      IsDisabled: true,
      Duration: 2
    },
    {
      Key: 311,
      Value: "SGH",
      IsDisabled: false,
      Duration: 9
    },
    {
      Key: 309,
      Value: "LOC",
      IsDisabled: true,
      Duration: 4
    },
    {
      Key: 485,
      Value: "TT",
      IsDisabled: true,
      Duration: 5
    },
    {
      Key: 310,
      Value: "FOT",
      IsDisabled: true,
      Duration: 6
    },
    {
      Key: 314,
      Value: "TS",
      IsDisabled: true,
      Duration: 7
    }
  ],

  [{
      Key: 313,
      Value: "R/I",
      IsDisabled: true,
      Duration: 1
    },
    {
      Key: 312,
      Value: "MEL",
      IsDisabled: true,
      Duration: 2
    },
    {
      Key: 311,
      Value: "SGH",
      IsDisabled: true,
      Duration: 3
    },
    {
      Key: 309,
      Value: "LOC",
      IsDisabled: false,
      Duration: 6
    },
    {
      Key: 485,
      Value: "TT",
      IsDisabled: true,
      Duration: 5
    },
    {
      Key: 310,
      Value: "FOT",
      IsDisabled: true,
      Duration: 6
    },
    {
      Key: 314,
      Value: "TS",
      IsDisabled: true,
      Duration: 7
    }
  ]
];

var allTasks = [].concat.apply([], parentArray);
var grouped = {};
for (var i = 0, len = allTasks.length; i < len; i++) {
  var item = allTasks[i];  
  grouped[item["Key"]] = grouped[item["Key"]] || [];
  grouped[item["Key"]].push(item);
};

console.log(grouped);

答案 2 :(得分:1)

请尝试以下过程。

  1. 连接两个数组
  2. 通过分析级联数组创建新数组。

<!DOCTYPE html>
<html lang="en">
  <head>
    <script>
      var arr1 = [
        {TaskType : {Key: 313, Value: "R/I", IsDisabled: true, Duration: 1}},
        {TaskType : {Key: 312, Value: "MEL", IsDisabled: true, Duration: 2}},
        {TaskType : {Key: 311, Value: "SGH", IsDisabled: false, Duration: 9}},
        {TaskType : {Key: 309, Value: "LOC", IsDisabled: true, Duration: 4}},
        {TaskType : {Key: 485, Value: "TT", IsDisabled: true, Duration: 5}},
        {TaskType : {Key: 310, Value: "FOT", IsDisabled: true, Duration: 6}},
        {TaskType : {Key: 314, Value: "TS", IsDisabled: true, Duration: 7}}
      ];
      var arr2 = [
        {TaskType : {Key: 313, Value: "R/I", IsDisabled: true, Duration: 1}},
        {TaskType : {Key: 312, Value: "MEL", IsDisabled: true, Duration: 2}},
        {TaskType : {Key: 311, Value: "SGH", IsDisabled: true, Duration: 3}},
        {TaskType : {Key: 309, Value: "LOC", IsDisabled: false, Duration: 6}},
        {TaskType : {Key: 485, Value: "TT", IsDisabled: true, Duration: 5}},
        {TaskType : {Key: 310, Value: "FOT", IsDisabled: true, Duration: 6}},
        {TaskType : {Key: 314, Value: "TS", IsDisabled: true, Duration: 7}},
      ];
      var combinedArr = [];
      var joinedArray = arr1.concat(arr2);
      joinedArray.forEach(item => {
        if(combinedArr[item.TaskType.Key]){
          combinedArr[item.TaskType.Key].push(item)
        }
        else {
          combinedArr[item.TaskType.Key] = [];
          combinedArr[item.TaskType.Key].push(item)
        }
      });
      console.log(combinedArr);
      </script>
  </head>
</html>