0

I have a JSON object which I load using $http.get like this:

$http.get("getData.php")//getData.php returns a J son file
    .success(function(response) {$scope.data = response;});

The JSON looks like this:

{
    "1": {
        "1": "*/3 * * * *",
        "2": "*/6 * * * *",
        "3": "*/3 * * * *",
        "4": "* * * * *"
    },
    "2": {
        "1": "*/3 * * * *",
        "2": "*/2 * * * *",
        "3": "*/3 * * * *",
        "4": "* * * * *"
    }
}

How can I pass this object to a function under the same controller and access its content?

I tried a few variations, most intuitive was this:

   $scope.compare = function (data){
        return data[1][1];
    }

But none of my attempts worked.

2 Answers 2

3

data[0][0] does not reference any items in your JSON object.

data[1][1] would return "*\/3 * * * *"
data[1][2] would return "*\/6 * * * *"

In the square brackets, you need to use the keys you defined in the JSON object because this data is an object, not an array.

Simple Demo

Sign up to request clarification or add additional context in comments.

6 Comments

Without seeing your full application running, this will be difficult to debug (your issue could be that data is being referenced in the function before it gets a value assigned to it). Can you check in your function to ensure data actually has a value? (i,e, console.log(data);... this will tell you if data is being passed properly.)
guessing its kind of stupid but how can i get the number of the array?
I played around with the circumstance of your application in angular a bit and I think if you handle your compare function a little differently it might work better: $scope.compare = function() { return $scope.data[1][1]; } DEMO
@Al.s what do you mean by the 'number of the array'?
sorry i ment the sub-array number, there is sub-array '1' and '2', im trying to get this number with ng-repeat but im only able to get the values inside those sub-array's
|
1

The array starts with "1".

Try:

data["1"]["1"]

or

data[1][1]

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.