how to get random element of array which is not null?

Hyrule

Let's say i haven an array of let arr = [null, null, null, null, null, null, null, null, null] containing 9 items.

And i have 9 <button data-index> with each having an data-index=0 to data-index=9

If i click a button, let's say <button data-index=4>, then we assign 'Cookie' to the corresponding index of arr: arr[4] = 'Cookie'

arr becomes [null, null, null, null, 'Cookie', null, null, null, null]

How can i select a random element/index of arr which is null without it selecting arr[4] since it already contains Cookie?.

It's for the AI move of a tic-tac-toe game.

Alberto Trindade Tavares

An ES6 solution with filter:

var arr = [null, null, null, null, 'Cookie', null, null, null, null];

var indexes = Array.from(Array(arr.length).keys());
var availableIndexes = indexes.filter((index) => arr[index] == null);
var selectedIndex = availableIndexes[Math.floor(Math.random()* availableIndexes.length)];

console.log(availableIndexes);
console.log(selectedIndex);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get which element was clicked?

分類Dev

how to check in pandas if element is in column which is an array

分類Dev

How to get element by attribute name which ends with defined word

分類Dev

How to get the size of an element inside an array?

分類Dev

How to get the i'th element of an associative array?

分類Dev

Pick random element from an array and remove it

分類Dev

How to separate an <a> element which is inside a <p> element

分類Dev

How to get array's element type size in D?

分類Dev

Get element from which onclick function is called

分類Dev

Get the element (node) which triggered an event

分類Dev

How to get all element from div block which contains type=radio?

分類Dev

How do I get all item of an array which doesn't exists in another array

分類Dev

How to get a random enum in TypeScript?

分類Dev

get element in array based on element's attributes

分類Dev

how to get random row values from a table and storing it in an array to be called individually

分類Dev

Get Array element on button click

分類Dev

How to delete array of element in array

分類Dev

get an element from array with http GET php

分類Dev

How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

分類Dev

Generate an array in PHP of random number not close to the X previous element

分類Dev

Selecting a random element of array to set a Youtube videoId API

分類Dev

How to modify a slice element which is a struct in golang?

分類Dev

Get random element from redis set without duplicate

分類Dev

How to replace random parts of string with random keys from array?

分類Dev

How to know which variable has a value and which one is null?

分類Dev

Find three elements in a sorted array which sum to a fourth element

分類Dev

Find the maximum element in an array which is first increasing and then decreasing

分類Dev

How to find the random value for a particular string in an array

分類Dev

Get an element of a String, which looks like XML and manipulate it in java

Related 関連記事

  1. 1

    How to get which element was clicked?

  2. 2

    how to check in pandas if element is in column which is an array

  3. 3

    How to get element by attribute name which ends with defined word

  4. 4

    How to get the size of an element inside an array?

  5. 5

    How to get the i'th element of an associative array?

  6. 6

    Pick random element from an array and remove it

  7. 7

    How to separate an <a> element which is inside a <p> element

  8. 8

    How to get array's element type size in D?

  9. 9

    Get element from which onclick function is called

  10. 10

    Get the element (node) which triggered an event

  11. 11

    How to get all element from div block which contains type=radio?

  12. 12

    How do I get all item of an array which doesn't exists in another array

  13. 13

    How to get a random enum in TypeScript?

  14. 14

    get element in array based on element's attributes

  15. 15

    how to get random row values from a table and storing it in an array to be called individually

  16. 16

    Get Array element on button click

  17. 17

    How to delete array of element in array

  18. 18

    get an element from array with http GET php

  19. 19

    How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

  20. 20

    Generate an array in PHP of random number not close to the X previous element

  21. 21

    Selecting a random element of array to set a Youtube videoId API

  22. 22

    How to modify a slice element which is a struct in golang?

  23. 23

    Get random element from redis set without duplicate

  24. 24

    How to replace random parts of string with random keys from array?

  25. 25

    How to know which variable has a value and which one is null?

  26. 26

    Find three elements in a sorted array which sum to a fourth element

  27. 27

    Find the maximum element in an array which is first increasing and then decreasing

  28. 28

    How to find the random value for a particular string in an array

  29. 29

    Get an element of a String, which looks like XML and manipulate it in java

ホットタグ

アーカイブ