I have an array with list of elements.
app.controller("MainController", function($scope){
$scope.names= [
{
value: "q1"
},
{
value: "q2"
},
{
value: "q3"
}
];
});
I need to take randomly two elements and assign to a new array. how can i do?
You must use only javascript to get this done. Because AngularJS is not a programming language.
Check this out:
if (!Math.getRandomValueBetween) {
Math.getRandomValueBetween = function (from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
};
}
//USAGE -- Math.getRandomValueBetween(100,1000) //950
if (!Array.prototype.getRandom) {
Array.prototype.getRandom = function () {
return this[Math.getRandomValueBetween(0, this.length - 1)];
};
}
//USAGE -- [1,34,56,76,9,67,5].getRandom();
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.