0

I have the following sample json:

{
    "camp": [
        {
            "name": "Name",
            "data": [
                {
                    "date": "04/08/2014",
                    "value": 1000
                },
                {
                    "date": "05/08/2014",
                    "value": 1110
                }
            ]
        }
    ]
}

Here, I'm able to do: model.get("camp")[0], but when I try: model.get("camp")[0].get("data"), I get the following error:

undefined is not a function 

Here model is the standard backbone model which extends Backbone.Model I'm confused what I'm doing wrong !!

4
  • What exactly is obj in the first place, what exactly does obj.get() return? Commented Aug 7, 2014 at 8:39
  • 1
    Try this: obj["camp"][0]["data"] Commented Aug 7, 2014 at 8:42
  • 1
    It would be easy to get them like this obj.camp[0] or obj.camp[0]['data'] Commented Aug 7, 2014 at 8:45
  • When you say you are able to do model.get("camp")[0], what does it give you? (try a console.log with it). Also, what does console.log(model["camp"]); output for you? Commented Aug 7, 2014 at 8:51

3 Answers 3

2

You only need to call the model.get() function once. After that, you can treat the returned object just like any other javascript-object. For example, you could do this to get one of the values deep inside the object:

model.get("camp")[0].data[0].value

To achieve what you are trying to get, do this:

model.get("camp")[0].data
Sign up to request clarification or add additional context in comments.

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
@GeraldSchneider: Even if this answer is short, it totally answer the question...
I've expanded my answer to make it more clear. Thanks for bringing this to my attention.
1

If you want to acces a property of your json array, you should simply do like this:

var test = json.camp.name

2 Comments

json array? json is just text... you need an object if you want to access any of it
yep sorry json is just formated text. But i'm sure you can access properties like i wrote.
-1

This is how you get your value:

obj.camp[0].data[0]

1 Comment

Thanks :) Just a newbie.

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.