重新排列阵列中的对象

时间:2018-12-07 12:13:54

标签: javascript arrays node.js express

我正在尝试重新排列以下数组。其实排序是 基于sourcelocation.orderindex,每个对象都有其对应的 另一个对象(与rider_id相同)以及is_dropoff字段true,我想以以下方式对数组重新排序。

第1步:sourceindex0的对象应放在顶部 案例前3个对象将位于最前面,下一个对象必须比sourcelocation.order0,并且is_dropoff为真,请注意,destinationlocation.orderindex不能大于即将出现的对象{{1} }值。

sourcelocation.orderindex

以下必须是我期望的答复

let inputSet = [
  {
    "sourcelocation": {
      "orderindex": "0"
    },
    "destinationlocation": {
      "orderindex": "1"
    },
    "rider_ids": "2b7116ea3dead9870b828a1v"
  },
  {
    "sourcelocation": {
      "orderindex": "0"
    },
    "destinationlocation": {
      "orderindex": "1"
    },
    "rider_ids": "4b7116ea3dead9870b828a19"
  },
  {
    "sourcelocation": {
      "orderindex": "0"
    },
    "destinationlocation": {
      "orderindex": "3"
    },
    "rider_ids": "548e653d56060c83838b772e"
  },
  {
    "rider_ids": "2b7116ea3dead9870b828a1v",
    "is_dropoff": true,
    "address": {
      "orderindex": "1"
    }
  },
  {
    "rider_ids": "4b7116ea3dead9870b828a19",
    "is_dropoff": true,
    "address": {
      "orderindex": "1"
    }
  },
  {
    "rider_ids": "548e653d56060c83838b772e",
    "is_dropoff": true,
    "address": {
      "orderindex": "3"
    }
  },
  {
    "sourcelocation": {
      "orderindex": "2"
    },
    "destinationlocation": {
      "orderindex": "3"
    },
    "rider_ids": "5b8e676d56060c83838b772e"
  },
  {
    "rider_ids": "5b8e676d56060c83838b772e",
    "is_dropoff": true,
    "address": {
      "orderindex": "3"
    }
  }
]

0 个答案:

没有答案