How to draw objects from an array?

Asked

Viewed 2,267 times

0

I have in the variable optionsPergunta an object array and need to draw the position of the objects in this array:

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

I tried something like:

optionsPergunta.sort(this.sorteiaArray)

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

But when showing the content of optionsPergunta, I remain with the objects in the same position.

  • 2

    Maybe it’s not clear to me, it’s all positions or you want to take a position at random?

  • I believe that by "draw" you mean "order". If so, by which field would you like to order?

3 answers

1


You can with Array destructuring.

Following example:

//gerar um Array com 30 itens para teste
const arr = [];

for (let i = 0; i < 30; i++) {
  arr.push({id: i, item: `Item: ${i}`});
}       

//Código para ordenar o Array de forma aletória
for (let i = 0; i < arr.length; i++) {
 const j = Math.floor(Math.random() * (i + 1));
 [arr[i], arr[j]] = [arr[j], arr[i]];
}
         
console.log(arr);

  • That’s exactly what I needed.

1

When it comes to random numbers, you can use the Math.Random() method, which has a numbering between zero(0) and one(1), e.g.: 1.x numbers or 0.x numbers, in order not to bring only this variant and go through the whole array, we can use a function, and on your return we can round down, for this we use Math.floor()

radom = (max, min) => { 
    let valor = Math.random() * (max - min) + min
    return Math.floor(valor)
}
console.log(radom(options.length,0))

in relation to the Sort function, it sorts the array: array.prototype.Sort()

1

The idea here is while the array options has elements, will be drawn a number (from zero to last Dice) that will be the element of index to remove from the array options. Then added to the optionsPergunta

const options = [
    { "id": 6 },
    { "id": 5 },
    { "id": 7 },
    { "id": 8 }
];

const optionsPergunta = [];

while (options.length) {

    const index = Math.floor(Math.random() * options.length - 1);

    const [option] = options.splice(index, 1);

    optionsPergunta.push(option);
}

console.log(optionsPergunta);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.