-2

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?

user2280016
  • 1,799
  • 3
  • 13
  • 17

1 Answers1

1

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();

From KnightCoder gist

Temp O'rary
  • 5,366
  • 13
  • 49
  • 109