How to choose a random Value from Assoc array then display the key and value

Taylor Cawiezell

Hello I am trying to display a random value key pair separately in a string. I want the value and key to remain together.

charAT = {
         'Flamethrower' : Math.floor(Math.random()*(15-5+1)+5),
         'Headbut' : Math.floor(Math.random()*(5-3+1)+3),
         'Fireblast' : Math.floor(Math.random()*(25-10+1)+10),
         'Tailwhip': 0
     };

Want this but for assoc array

rand = charAT[Math.floor(Math.random() * charAT.length)];

Example Code Wanted

alert('charizard used '+ rand:key + 'and did ' + rand:value + ' damage!')

Wanted Output

charizard used flamethrower and did 12 damage!

Thanks in advance!

rnrneverdies

You can use Object.keys() to get a array filled with the object property names. So, applying a random index on that array, you can get a random property name, then you can use it to get the random property value desired.

 var charAT = {
     'Flamethrower' : Math.floor(Math.random()*(15-5+1)+5),
     'Headbut' : Math.floor(Math.random()*(5-3+1)+3),
     'Fireblast' : Math.floor(Math.random()*(25-10+1)+10),
     'Tailwhip': 0
 };

 var ix = Math.floor(Math.random() * Object.keys(charAT).length);
 var rand = Object.keys(charAT)[ix];
 alert(rand + ":" + charAT[rand]);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to display the highest value in an array

분류에서Dev

How to choose a random SKShapeNode from an array using arc4random?

분류에서Dev

How to sort and display JSON objects based on one key value in php

분류에서Dev

StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How?

분류에서Dev

How to remove a simple specific key value pair from all objects inside an array

분류에서Dev

How to create a comma-delimited string from associative array's key-value pairs

분류에서Dev

How do I create an array using the order and value from one and the key of another?

분류에서Dev

Get array of key/value pairs from document in mongodb

분류에서Dev

How to choose attribute for a tag with no value in javascript

분류에서Dev

How to output a value from array Mongoose?

분류에서Dev

how to delete array value from table in jquery

분류에서Dev

How to get value from user in java for array?

분류에서Dev

How to update a key's value in an associative array in php

분류에서Dev

How to get an array which is the value of a Dictionary key in Swift?

분류에서Dev

Extract value from NSObject key/value pair

분류에서Dev

Obtaining an array element based on a random value

분류에서Dev

How to display the selected value from the dropdown menu using JQuery?

분류에서Dev

From string to Dictionary key and value

분류에서Dev

make one array the key and another array the value

분류에서Dev

how to create variables from key value object pair in javascript

분류에서Dev

How to get auto generated value from composite primary key in hibernate

분류에서Dev

set table random value from table

분류에서Dev

how to display database value in textfield when user select value from drop down list

분류에서Dev

How to check for a value in an array

분류에서Dev

Search mutlidimensional array for a value and return key in PHP

분류에서Dev

preg_match part of an array key value

분류에서Dev

Dictionary key-value using with array

분류에서Dev

Hashtable Key and value storing in separate array list

분류에서Dev

PHP logical key/value array separators

Related 관련 기사

  1. 1

    How to display the highest value in an array

  2. 2

    How to choose a random SKShapeNode from an array using arc4random?

  3. 3

    How to sort and display JSON objects based on one key value in php

  4. 4

    StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How?

  5. 5

    How to remove a simple specific key value pair from all objects inside an array

  6. 6

    How to create a comma-delimited string from associative array's key-value pairs

  7. 7

    How do I create an array using the order and value from one and the key of another?

  8. 8

    Get array of key/value pairs from document in mongodb

  9. 9

    How to choose attribute for a tag with no value in javascript

  10. 10

    How to output a value from array Mongoose?

  11. 11

    how to delete array value from table in jquery

  12. 12

    How to get value from user in java for array?

  13. 13

    How to update a key's value in an associative array in php

  14. 14

    How to get an array which is the value of a Dictionary key in Swift?

  15. 15

    Extract value from NSObject key/value pair

  16. 16

    Obtaining an array element based on a random value

  17. 17

    How to display the selected value from the dropdown menu using JQuery?

  18. 18

    From string to Dictionary key and value

  19. 19

    make one array the key and another array the value

  20. 20

    how to create variables from key value object pair in javascript

  21. 21

    How to get auto generated value from composite primary key in hibernate

  22. 22

    set table random value from table

  23. 23

    how to display database value in textfield when user select value from drop down list

  24. 24

    How to check for a value in an array

  25. 25

    Search mutlidimensional array for a value and return key in PHP

  26. 26

    preg_match part of an array key value

  27. 27

    Dictionary key-value using with array

  28. 28

    Hashtable Key and value storing in separate array list

  29. 29

    PHP logical key/value array separators

뜨겁다태그

보관