I am faced with a challenge involving two Javascript JSON arrays. The arrays in question are named this.BicyclePartsOLD
and this.BicyclePartsNEW
.
Both arrays contain an attribute named "ListOrder". The OLD array is currently ordered from ListOrder 1 to n items.
The NEW array has been modified but still contains the same records as the BicyclePartsOLD. Now, I need to update the OLD array based on changes made in the NEW array. For example, if someone changed the ListOrder
from 1 to 3 in the NEW array, I need to update the OLD array accordingly by setting ListOrder 1 to 3, ListOrder 2 to 1, and ListOrder 3 to 2.
I have attempted the following approach, but I am unsure about the best way to rearrange the ListOrder values:
for(var i = 0; i < this.BicyclePartsOLD.length; i++)
{
for(var j = 0; j < this.BicyclePartsNEW.length; j++)
{
if(this.BicyclePartsOLD[i].PartNumber === this.BicyclePartsNEW[j].PartNumber)
{
this.BicyclePartsOLD[i].ListOrder = this.BicyclePartsNEW[j].ListOrder;
//NOT Sure how to reorder BicyclePartsOLD here, there will be another
//item with the same ListOrder at this point.
}
}
}
If anyone could provide guidance or advice to help me navigate towards the right solution, it would be greatly appreciated.