[英]Sort objects inside a array
我的變量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"
}
]
長話短說。 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))
顯然,您不需要對數組進行排序,而是對其進行洗牌。
通過使用返回隨機值的比較函數對數組進行排序來混洗數組並不是一個好主意。 對於某些排序算法,它會導致無法保證結束的循環。
如果您需要對數組進行混洗,有一種更好(並且可能更快)的方法:從數組中隨機選擇一項,將其從數組中刪除並將其附加到新數組(混洗后的數組)中。 代碼可能如下所示:
// 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]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.