J'ai 2 tableaux dans un autre tableau. À l'intérieur de ces tableaux, il y a des objets et je veux les regrouper par la clé d'objet.
le tableau parent contient n tableau
0: 309: [TaskType,TaskType] 310: [TaskType,TaskType] 311: [TaskType,TaskType] 312: [TaskType,TaskType] 313: [TaskType,TaskType] 314: [TaskType,TaskType] 485: [TaskType,TaskType]
Comme je l'ai dit , à l'intérieur de ces tableaux, il y a aussi un tableau rempli d'objets
tableau 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: 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}
tableau 2 em>
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}
Je souhaite regrouper ces tableaux par leurs clés. Le résultat devrait ressembler à ceci:
[array_1, array_2]
Comment faire cela?
3 Réponses :
Une façon de le faire est de fusionner les tableaux ensemble en premier et la clé par chaque valeur de clé:
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);
Vous pouvez utiliser l'opérateur spread
pour concater des tableaux, puis utiliser la méthode reduction
pour regrouper par clés
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);
Essayez la procédure suivante.
<!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>