Mark Mark - 4 months ago 6x
Javascript Question

Changing variable value by next element in array

I have an array with some text. One variable is set to first element in the array, how can I set that function can change that variable change value to value from next element in an array?

var q= [{qs: 'JR?', id: 1},
{qs: 'DR?', idp: 2},
{qs: 'MR ?', idp: 3},
{qs: 'SR ?', idp: 4},
{qs: 'LR ?', idp: 5} ];
$scope.qes= q[0].qs;
$scope.check= function(){
$scope.qes = q[0].qs + 1; // something like that


Your question is not enough clear.

As per your array initially you have 'DR?' in " $scope.check" variable. If you want 'MR?' as the next then do the following

You need to change the current element of the array. Suppose if as default a pointer points the first element of the array then the initial position will be 0. And all you need to do to increment the position value by 1.

//first take a pointer set to 0 [as here 0 is default position]

    $scope.pos= 0;
    $scope.check= function(){
       //change position to next 
       $scope.qes = q[$scope.pos].qs;