I'm struggling with implementing a nested for loop to sort items in an array. Unfortunately, I'm encountering an issue where some values are being duplicated and I can't seem to identify the root cause of the problem. My logical reasoning skills are not fully developed yet. Even though I am aware of using built-in sorting functions, this task is a part of my school assignment which requires the use of two for loops. My intention is to utilize the "order" key value to determine the position of objects in the array.
var connection = await fetch('momondo.php')
var jData = await connection.json()
var aScheduleInOrder = jData
for (var i = 0; i < jData.length; i++) {
for (var j = 0; j < jData[i].schedule.length; j++) {
aScheduleInOrder[i].schedule[jData[i].schedule[j].order] = jData[i].schedule[j]
}
}
Below are the JSON objects contained in the jData:
[
{
"currency":"DKK",
"price":3000,
"schedule":
[
{"airlineIcon":"LH.png", "date":1581513600,"id":"SAS22","from":"C", "to":"D","waitingTime":120,"flightTime":240,"order":2},
{"airlineIcon":"KL.png","date":1581510000, "id":"SAS33","from":"B", "to":"C","waitingTime":60,"flightTime":180,"order":1},
{"airlineIcon":"DY.png","date":1581501600,"id":"SAS1","from":"A", "to":"B","waitingTime":0,"flightTime":80,"order":0}
]
},
{
"currency":"DKK",
"price":5000,
"schedule":
[
{"airlineIcon":"LX.png", "date":1581513600,"id":"SAS55","from":"B", "to":"C","waitingTime":120,"flightTime":240,"order":1},
{"airlineIcon":"SK.png","date":1581501600,"id":"SAS44","from":"A", "to":"B","waitingTime":0,"flightTime":80,"order":0}
]
},
{
"currency":"DKK",
"price":15000,
"schedule":
[
{"airlineIcon":"AC.png","date":1581501600,"id":"SAS78","from":"A", "to":"B","waitingTime":0,"flightTime":80,"order":0}
]
}
]
In the array at position [1], within the schedule array, the first item at position [0] has an "order" key with a value of 1. According to my logic utilizing two for loops, this item should be moved to position [1], and subsequent items should follow suit accordingly. The updated order should resemble the following structure:
[
{
"currency":"DKK",
"price":3000,
"schedule":
[
{"airlineIcon":"DY.png","date":1581501600,"id":"SAS1","from":"A", "to":"B","waitingTime":0,"flightTime":80,"order":0},
{"airlineIcon":"KL.png","date":1581510000, "id":"SAS33","from":"B", "to":"C","waitingTime":60,"flightTime":180,"order":1},
{"airlineIcon":"LH.png", "date":1581513600,"id":"SAS22","from":"C", "to":"D","waitingTime":120,"flightTime":240,"order":2}
]
},
{
"currency":"DKK",
"price":5000,
"schedule":
[
{"airlineIcon":"SK.png","date":1581501600,"id":"SAS44","from":"A", "to":"B","waitingTime":0,"flightTime":80,"order":0},
{"airlineIcon":"LX.png", "date":1581513600,"id":"SAS55","from":"B", "to":"C","waitingTime":120,"flightTime":240,"order":1}
]
},
{
"currency":"DKK",
"price":15000,
"schedule":
[
{"airlineIcon":"AC.png","date":1581501600,"id":"SAS78","from":"A", "to":"B","waitingTime":0,"flightTime":80,"order":0}
]
}
]
Your assistance in resolving this dilemma would be greatly appreciated :)