[英]Group objects inside the nested array
我在另一个数组中有 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]
怎么能做到这一点?
您可以使用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);
一种方法是先将数组合并在一起,然后按每个键值键:
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);
请尝试以下过程。
<!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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.