Pobierz losowy element z tablicy JavaScript [duplikat]
var items = Array(523, 3452, 334, 31, ..., 5346);
Jak zdobyć losowy przedmiot z items
?
13 answers
var item = items[Math.floor(Math.random() * items.length)];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-03-26 14:06:08
Użyj podkreślenia (lub loDash :)):
var randomArray = [
'#cc0000','#00cc00', '#0000cc'
];
// use _.sample
var randomElement = _.sample(randomArray);
// manually use _.random
var randomElement = randomArray[_.random(randomArray.length-1)];
Lub przetasować całą tablicę:
// use underscore's shuffle function
var firstRandomElement = _.shuffle(randomArray)[0];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-17 10:31:18
Jeśli naprawdę musisz użyć jQuery, aby rozwiązać ten problem (NB: nie powinieneś):
(function($) {
$.rand = function(arg) {
if ($.isArray(arg)) {
return arg[$.rand(arg.length)];
} else if (typeof arg === "number") {
return Math.floor(Math.random() * arg);
} else {
return 4; // chosen by fair dice roll
}
};
})(jQuery);
var items = [523, 3452, 334, 31, ..., 5346];
var item = jQuery.rand(items);
Ta wtyczka zwróci element losowy, Jeśli otrzyma tablicę lub wartość z [0 .. n) podane liczby, lub biorąc pod uwagę cokolwiek innego, gwarantowaną wartość losową!
Dla dodatkowej zabawy, zwracanie tablicy jest generowane przez wywołanie funkcji rekurencyjnie na podstawie długości tablicy:)
Demo robocze w http://jsfiddle.net/2eyQX/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-07 09:48:19
1. rozwiązanie: define array prototype
Array.prototype.random = function () {
return this[Math.floor((Math.random()*this.length))];
}
Które będą działać na tablicach inline
[2,3,5].random()
I oczywiście predefiniowane tablice
list = [2,3,5]
list.random()
2. rozwiązanie: zdefiniuj własną funkcję, która akceptuje list i zwraca element
get_random = function (list) {
return list[Math.floor((Math.random()*list.length))];
}
get_random([2,3,5])
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-28 06:43:43
Oto jeszcze inny sposób:
function rand(items) {
// "~~" for a closest "int"
return items[~~(items.length * Math.random())];
}
Lub zgodnie z zaleceniami poniżej przez @1248177:
function rand(items) {
// "|" for a kinda "int div"
return items[items.length * Math.random() | 0];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-23 21:58:48
var random = items[Math.floor(Math.random()*items.length)]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-06 17:50:39
JQuery to JavaScript! To tylko framework JavaScript. Aby znaleźć przypadkowy element, użyj zwykłego starego JavaScript, na przykład
var randomItem = items[Math.floor(Math.random()*items.length)]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-19 21:05:39
var rndval=items[Math.floor(Math.random()*items.length)];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-06 17:50:25
// 1. Random shuffle items
items.sort(function() {return 0.5 - Math.random()})
// 2. Get first item
var item = items[0]
Krótszy:
var item = items.sort(function() {return 0.5 - Math.random()})[0];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-28 07:01:51
var items = Array(523,3452,334,31,...5346);
function rand(min, max) {
var offset = min;
var range = (max - min) + 1;
var randomNumber = Math.floor( Math.random() * range) + offset;
return randomNumber;
}
randomNumber = rand(0, items.length - 1);
randomItem = items[randomNumber];
Kredyt:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-22 22:02:31
Jeśli używasz node.js, możesz użyć unique-random-array. Po prostu wybiera coś losowego z tablicy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-18 00:04:57
const ArrayRandomModule = {
// get random item from array
random: function (array) {
return array[Math.random() * array.length | 0];
},
// [mutate]: extract from given array a random item
pick: function (array, i) {
return array.splice(i >= 0 ? i : Math.random() * array.length | 0, 1)[0];
},
// [mutate]: shuffle the given array
shuffle: function (array) {
for (var i = array.length; i > 0; --i)
array.push(array.splice(Math.random() * i | 0, 1)[0]);
return array;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-29 15:47:47
Alternatywnym sposobem byłoby dodanie metody do prototypu tablicy:
Array.prototype.random = function (length) {
return this[Math.floor((Math.random()*length))];
}
var teams = ['patriots', 'colts', 'jets', 'texans', 'ravens', 'broncos']
var chosen_team = teams.random(teams.length)
alert(chosen_team)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-19 21:06:00