for循环内部for循环对项目进行排序

时间:2020-02-12 09:55:32

标签: javascript arrays json

我正在尝试在for循环内使用for循环对数组中的项目进行排序,但是有些值却重复了,我似乎无法发现问题所在。我的逻辑还不完善。我知道有排序功能,但这是学校作业的一部分,我们必须使用两个for循环。我希望使用“ order”键的值来确定对象的数组位置。

 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]
    }
  }

这是jData中的json对象:

[  
  {     
  "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}
  ]
}    
]

在数组的位置[1]的时间表数组中,第一个项目[0]的位置有一个名为“ order”的键,其值为1。这应该按照我的逻辑,其中两个用于循环,将其移至位置[1],并将位于位置[1]的第二项移至位置[2],以此类推。这是下面的样子:

[  
  {     
  "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}
  ]
}      
]

我希望你们能帮助我:)

2 个答案:

答案 0 :(得分:1)

为什么不使用.sort()数组方法? 编辑:Aaaah this is part of a school assignment and we have to use the two for loops.学校作业FTW

let jData = [{
    "currency": "DKK",
    "price": 3000,
    "schedule": [{
        "airlineIcon": "SK.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": "SAS1",
        "from": "A",
        "to": "B",
        "waitingTime": 0,
        "flightTime": 80,
        "order": 1
      },
      {
        "airlineIcon": "LH.png",
        "date": 1581513600,
        "id": "SAS22",
        "from": "B",
        "to": "C",
        "waitingTime": 120,
        "flightTime": 240,
        "order": 0
      }
    ]
  },

  {
    "currency": "DKK",
    "price": 15000,
    "schedule": [{
      "airlineIcon": "SK.png",
      "date": 1581501600,
      "id": "SAS1",
      "from": "A",
      "to": "B",
      "waitingTime": 0,
      "flightTime": 80,
      "order": 0
    }]
  }
];

jData = jData.map( obj => {
    obj.schedule.sort( (a,b) => a.order > b.order ? 1 : -1); // sort by the "order" key ascending
    return obj;
})

console.log(jData)

答案 1 :(得分:1)

好吧,很清楚,您只想对order属性进行排序,而不是对数组中的项进行排序?然后在第二个嵌套循环中添加以下内容

aScheduleInOrder[i].schedule[j].order = j;

代替

aScheduleInOrder[i].schedule[jData[i].schedule[j].order] = jData[i].schedule[j]

编辑:原始问题更改后,特此提供新答案。请尝试此代码,它将从头开始创建新阵列,并在原始阵列中查找正确的调度顺序。从老师的角度来看也很容易理解:

for (var i = 0; i < jData.length; i++) {

    var currency = jData[i].currency;
    var price = jData[i].price;
    var schedule = [];

    var data = {
        "currency": currency,
        "price":    price,
        "schedule" : []
    };

    for (var j = 0; j < jData[i].schedule.length; j++) {
        data.schedule[j] = getScheduleByOrder(jData[i].schedule,j);
    }

    aScheduleInOrder.push(data);
}

function getScheduleByOrder(scheduleArray,order){

    for (var k = 0; k < scheduleArray.length; k++) {
        if (scheduleArray[k].order === order){
            return scheduleArray[k];            
        }
    }   
}