1

I have 2 arrays inside another array. Inside these arrays there are objects and I want to group them by the object key.

parent array contains n array

[array_1, array_2]

Like I said, inside these arrays there is also an array filled with objects

array 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}

array 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}

I want to group these arrays by their keys. Result should be like this:

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

How can do this?

0

3 Answers 3

2

You can use spread operator to concat arrays and then use reduce method to group by keys

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try the following procedure.

  1. Concatenate the two arrays
  2. Create a new array by parsing the concatenated array.

<!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>

Comments

1

One way to do it is to merge the arrays together first and key by each Key value:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.