2

I have this code :

$http(req).then(function success(response) {
    // $scope.table = { fields: [1,2] };
    $scope.table = { fields: [] };
    for (var i = 0; i < 9; i++) {
        // add elements from response to $scope.table
    }
});

The question is: how do I add elements to my variable $scope.table?
As we know, variables in JavaScript are not strongly-typed. I can't find any documentation that explains how to manage a type generated by something like this { ID: [] }.

2 Answers 2

4

Actually, the variable $scope.table is an Object which has an array property (at key) fields. With that array, use array.prototype.push() to add elements to the end of the array.

e.g.

$http(req).then(function success(response) {
// $scope.table = { fields: [1,2] };
$scope.table = { fields: [] };
for (var i = 0; i < 9; i++) {
    // add elements from response to $scope.table
    $scope.table.fields.push(response[i]);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Sam, thanks for the explanation, your solution worked perfectly!
3

It doesn't have anything to do with JS being strongly-typed or not.
Simply access the fields array using the notion below:

$scope.table.fields.push(i);

Put it inside your for loop.

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.