对数组内的对象进行排序

时间:2019-12-27 19:21:47

标签: javascript node.js

我的变量optionsPergunta中有一个需要排序的对象数组options

我尝试过类似的事情:

optionsPergunta.sort(this.sorteiaArray)

sorteiaArray(){
   return Math.random() < Math.random()
}

但是当我做console.log(optionsPergunta.options)时,位置没有改变

optionsPergunta是查询的结果,所以当我执行console.log(optionsPergunta)时,我得到以下结果:

[
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 6,
      book_unit_question_id: 2,
      description: 'get',
      image_sound: null,
      correct: true,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:27.000Z,
      updated_at: 2019-12-27T19:06:27.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 6,
      book_unit_question_id: 2,
      description: 'get',
      image_sound: null,
      correct: true,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:27.000Z,
      updated_at: 2019-12-27T19:06:27.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  },
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 9,
      book_unit_question_id: 2,
      description: 'stay',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:45.000Z,
      updated_at: 2019-12-27T19:06:45.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 9,
      book_unit_question_id: 2,
      description: 'stay',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:45.000Z,
      updated_at: 2019-12-27T19:06:45.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  },
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 5,
      book_unit_question_id: 2,
      description: 'are',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:21.000Z,
      updated_at: 2019-12-27T19:06:33.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 5,
      book_unit_question_id: 2,
      description: 'are',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:21.000Z,
      updated_at: 2019-12-27T19:06:33.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  },
  BookUnitQuestionOption {
    __setters__: [
      '$attributes',
      '$persisted',
      'primaryKeyValue',
      '$originalAttributes',
      '$relations',
      '$sideLoaded',
      '$parent',
      '$frozen',
      '$visible',
      '$hidden'
    ],
    '$attributes': {
      id: 8,
      book_unit_question_id: 2,
      description: 'move',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:43.000Z,
      updated_at: 2019-12-27T19:06:43.000Z
    },
    '$persisted': true,
    '$originalAttributes': {
      id: 8,
      book_unit_question_id: 2,
      description: 'move',
      image_sound: null,
      correct: false,
      status: false,
      user_id: 1,
      created_at: 2019-12-27T19:06:43.000Z,
      updated_at: 2019-12-27T19:06:43.000Z
    },
    '$relations': {},
    '$sideLoaded': {},
    '$parent': null,
    '$frozen': false,
    '$visible': undefined,
    '$hidden': undefined
  }
]

当我返回结果并在邮递员中进行测试时,我有以下提示:

[
  {
    "id": 6,
    "book_unit_question_id": 2,
    "description": "get",
    "image_sound": null,
    "correct": true,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:27",
    "updated_at": "2019-12-27 16:06:27"
  },
  {
    "id": 9,
    "book_unit_question_id": 2,
    "description": "stay",
    "image_sound": null,
    "correct": false,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:45",
    "updated_at": "2019-12-27 16:06:45"
  },
  {
    "id": 5,
    "book_unit_question_id": 2,
    "description": "are",
    "image_sound": null,
    "correct": false,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:21",
    "updated_at": "2019-12-27 16:06:33"
  },
  {
    "id": 8,
    "book_unit_question_id": 2,
    "description": "move",
    "image_sound": null,
    "correct": false,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 16:06:43",
    "updated_at": "2019-12-27 16:06:43"
  }
]

2 个答案:

答案 0 :(得分:1)

简而言之。 optionsPergunta是指sort()来自Array.prototype.sort()的对象,这意味着您需要在数组上调用sort()。另一件事是sorteiaArray前面没有function,因此解释器虽然应该是函数调用。

const optionsPergunta = {
    "options": [
        {
            "id": 6,
            "book_unit_question_id": 2,
            "description": "get",
            "image_sound": null,
            "correct": true,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:27",
            "updated_at": "2019-12-27 16:06:27"
        },
        {
            "id": 5,
            "book_unit_question_id": 2,
            "description": "are",
            "image_sound": null,
            "correct": false,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:21",
            "updated_at": "2019-12-27 16:06:33"
        },
        {
            "id": 7,
            "book_unit_question_id": 2,
            "description": "go to",
            "image_sound": null,
            "correct": false,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:39",
            "updated_at": "2019-12-27 16:06:39"
        },
        {
            "id": 8,
            "book_unit_question_id": 2,
            "description": "move",
            "image_sound": null,
            "correct": false,
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-27 16:06:43",
            "updated_at": "2019-12-27 16:06:43"
        }
    ]
}

function sorteiaArray(){
   return Math.random() < Math.random()
}

console.log(optionsPergunta.options.sort(sorteiaArray))

答案 1 :(得分:0)

显然,您不需要对数组进行排序,而是可以对其进行随机排序。

使用返回随机值的比较函数对数组进行排序来改组数组不是一个好主意。对于某些排序算法,它会导致无法保证结束的循环。

如果需要改组数组,则有更好的方法(可能更快):从数组中随机选择一项,将其从数组中删除,然后将其追加到新的数组(改组后的数组)中。代码看起来像这样:

// Create a new array to not change the original
let source = optionsPergunta.slice();
// Store the shuffled items here
let shuffled = [];

while (source.length) {
  let index = Math.floor(Math.random() * Math.floor(source.length));
  let items = source.splice(index, 1);
  shuffled.push(items[0]);
}