mardi 8 octobre 2019

Select random entry from string list in javascript

Ok, so I have the following code which successfully generates a list (from an element);

this.elements('css selector', '#bfsDesktopFilters .search-filters__item #ddl-make > option', function (result) {
    result.value.forEach(element => {
        this.elementIdValue(element.ELEMENT, function (text) {
            var makeValue = text.value;
            console.log(makeValue);
        });
    });
})`

which results in a (long) list of bike makes, as below;

enter image description here etc, etc

My question is, how do I randomly select an entry from this list?

I've tried to split the results;

var elementMakesArray = makeValue.split('');
console.log(elementMakesArray);`

but this gave me the following;

enter image description here

I tried this;

var randomMake = Math.floor(Math.random() * makeValue);
console.log(randomMake);`

but got a NaN error.

So I'm just wondering how I can randomly select an entry from the list?

Any help would be greatly appreciated.

Thanks.




Aucun commentaire:

Enregistrer un commentaire