简体   繁体   中英

Javascript random array

var title = 'a', 'b';
var length = 326, '424';

how do I get a random pick from the options and make sure that if it picks 'a' then it needs to pick '326' with it and then how do you assign them a number?

For instances if it picks a it automatically needs to get 326 with it and then I need to give something like

 var title1 = (picked number) 
    var length = (picked with number)

and then the other it didn't pick I need it to make it

var title2 = (title it didn't pick)
var title2 = (didnt get picked length)

You can create your own object:

data=new Array();
data[0]=new Data('a',326);
data[1]=new Data('b',424);

function Data(title,length) {
    this.title=title;
    this.length=length;
    return this;
}

console.log(data);

Now you only need to choose a random element from the data array. To access the values use:

data[0].title
data[0].length

for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM