I have been given an array containing objects in the following format:
export interface Part {
workOrder?: string;
task?: string;
partNumber?: string;
qty?: number;
image?: string;
name?: string;
}
My goal is to loop through each object in the array and create a new array based on this interface
:
export interface WorkOrder {
workOrder?: string;
tasks?: Array<string>;
}
This is what my code currently looks like:
let partList: Part[] = [
{ workOrder: "W1", task: "do something", ... },
{ workOrder: "W1", task: "something else", ... },
{ workOrder: "W2", task: "do something", ... },
{ workOrder: "W2", task: "something else", ... }
];
let workOrders: WorkOrder[] = [];
I aim to populate the workOrders
array with the workOrder and task from each Part. If a workOrder has already been added, I want to append the additional task to the existing tasks array under that workOrder.
The final output should resemble this:
workOrders = [
{ workOrder: "W1", tasks: [ "do something", "something else" ] },
{ workOrder: "W2", tasks: [ "do something", "something else" ] }
];
Currently, I am using multiple for loops and checking the .indexOf()
method for certain arrays, but I believe there must be a more efficient approach.