0

I am trying to create a multi dimensional array in angular JS and push objects into it.

Below is my code snippet -

$scope.dm.Questionnaires.Q_options=[][][];
for(var i in data)
{
    if(data[i].dbObject.fieldname=="Q0001") {
        $scope.dm.Questionnaires.Q_options[1][1].push(data[i].dbObject);
    }
    else if(data[i].dbObject.fieldname=="Q0002") {
        $scope.dm.Questionnaires.Q_options[2][1].push(data[i].dbObject);
    }
}

Trying to access the array in html as below -

<select class="input-md form-control" ng-model="dm.Questionnaires.Q1000101" ng-options="item.antHillValue for item in dm.Questionnaires.Q_options[1][1] track by item.webServiceValue">
<option value="">select</option>
</select>

I am not able to get this working. Any help is appreciated.

1
  • 1
    Declaration of array is wrong Commented Jul 21, 2016 at 10:24

1 Answer 1

1

Use something like this.

$scope.dm = {};
$scope.dm.Questionnaires = {};
$scope.dm.Questionnaires.Q_options = [[[]]];

$scope.dm.Questionnaires.Q_options[0][0].push(1);

......

You must check if array at any index exists or not.

if ($scope.dm.Questionnaires.Q_options[index] == null ) {
      $scope.dm.Questionnaires.Q_options[index] = [];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.