0

I want to add a property to a singular element of an array.

Basically, I want to choose a singular element at random, make it appear on screen, then after it disappears add a "already chosen" property to that element so that it can't be randomly picked again.

Alternatively, if anyone knows how to temporarily exclude a single element from an array, that would be extremely helpful too.

const numbers =["1","2","3","4","5"]
  • Have you tried anything? `element.chosen = true` would probably work just fine – Jesper Oct 08 '21 at 09:16
  • If you want to make sure no element is picked twice you might as well just [*shuffle*](https://stackoverflow.com/q/2450954) the array once beforehand and iterate. – Yoshi Oct 08 '21 at 09:18
  • Please be more specific on what you have tried so others can understand better – Samuel Emeka Oct 08 '21 at 10:34

1 Answers1

0

you can use array of object this way

let values = [
  {
    "value": "1",
    "picked": "false",
  },
  {
    "value": "2",
    "picked": "false",
  }
  ,
  {
    "value": "3",
    "picked": "false",
  }
  ,
  {
    "value": "4",
    "picked": "false",
  }
  ,
  {
    "value": "5",
    "picked": "false",
  }
]

and every time that you pick a number give the true value to picked property

m.hatami
  • 603
  • 1
  • 7
  • 21