Two arrays with the same structure have been created.
let TaskArray=[
{"TaskID:"171","TaskName":"task1","TaskGroup":"group","UserID":"3"},
{"TaskID:"170","TaskName":"task2","TaskGroup":"group","UserID":"3"},
{"TaskID:"169","TaskName":"task3","TaskGroup":"group","UserID":"3"},
{"TaskID:"168","TaskName":"task4","TaskGroup":"group","UserID":"3"},
{"TaskID:"167","TaskName":"task5","TaskGroup":"group","UserID":"3"},
{"TaskID:"166","TaskName":"task6","TaskGroup":"group","UserID":"3"},
{"TaskID:"165","TaskName":"task7","TaskGroup":"group","UserID":"3"},
{"TaskID:"164","TaskName":"task8","TaskGroup":"group","UserID":"3"},
{"TaskID:"163","TaskName":"task9","TaskGroup":"group","UserID":"3"},
{"TaskID:"162","TaskName":"task10","TaskGroup":"group","UserID":"3"}
]
let TaskDetailsArray = [
{"TaskID:"171","TaskName":"task1","TaskGroup":"group","UserID":"3"},
{"TaskID:"170","TaskName":"task2","TaskGroup":"group","UserID":"3"},
{"TaskID:"169","TaskName":"task3","TaskGroup":"group","UserID":"3"},
{"TaskID:"168","TaskName":"task4","TaskGroup":"group","UserID":"3"},
]
A comparison needs to be made between the two arrays, and a flag named isAssigned should be set to true if an item from the second array is found based on its ID in the first array; otherwise false.
matcheArray = [
{"TaskID:"171","TaskName":"task1","TaskGroup":"group","UserID":"3",isAssigned: true},
{"TaskID:"170","TaskName":"task2","TaskGroup":"group","UserID":"3",isAssigned: true},
{"TaskID:"169","TaskName":"task3","TaskGroup":"group","UserID":"3",isAssigned: true},
{"TaskID:"168","TaskName":"task4","TaskGroup":"group","UserID":"3",isAssigned: true},
{"TaskID:"167","TaskName":"task5","TaskGroup":"group","UserID":"3"},
{"TaskID:"166","TaskName":"task6","TaskGroup":"group","UserID":"3"},
{"TaskID:"165","TaskName":"task7","TaskGroup":"group","UserID":"3"},
{"TaskID:"164","TaskName":"task8","TaskGroup":"group","UserID":"3"},
{"TaskID:"163","TaskName":"task9","TaskGroup":"group","UserID":"3"},
{"TaskID:"162","TaskName":"task10","TaskGroup":"group","UserID":"3"}
]
The current code functions as intended, but there may be alternative approaches. Any suggestions for improvement would be appreciated.
for (var i = 0; i < TaskArray.length; i++) {
for (var k = 0; k < this.TaskDetailsArray.length; k++) {
if (TaskArray[i].TaskID == this.TaskDetailsArray[k].TaskID) {
if (!this.TaskDetailsArray[k].isAssigned) {
this.TaskDetailsArray[k].isAssigned = true;
};
}
}