1

I'm trying to extract a certain name/value pair from a JSON object & assign it to a variable.

A sample of my object

{"new":[{"id":"185","title":"new time","slug":"new-time","time":"1363641168","text":"all done","deletetime":null}]

I'm trying to assign the time to a timestamp variable for later use in my AngularJS app. Anyway that I attempt, it returns as undefined.

$scope.news = data.new["time"];

What is the correct way to access the value of time?

2 Answers 2

3

new is a keyword in JavaScript, so you won't be able to use it in your property access notation. Consider renaming the new element in your data to be 'newItems' or something.

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

3 Comments

I didn't even think about that. It doesn't seem to cause a problem though. Could that be because it exists within an object that it doesn't cause an issue?
I went ahead and changed from the new keyword.
It doesn't seem to be a problem in this case actually. But its a good thing to remember.
1

"new" is an array, so the first item's time prop is:

data.new[0].time

1 Comment

Great! That seems like such an obvious answer. I was able to assign it to a variable without a problem.

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.