0

I make a call to an API

The API returns a result like:

{saved-jobs:[{/*item*/},{/*item*/}]}

I want to access each 'saved-jobs' like so:

success: function(result){
        $.each(result.saved-jobs, function(i, saved_job){
            console.log(saved_job.job_id)
        });
    },

only thing is, the ' - ' in saved-jobs causes an error. I don't have the ability to modify the API and what it returns. how can I get around it?

rpsep2
  • 2,901
  • 10
  • 34
  • 50

3 Answers3

3

You can access object properties using brackets too:

result['saved-jobs']

You can find more here: JavaScript property access: dot notation vs. brackets?

Community
  • 1
  • 1
Michał Miszczyszyn
  • 10,641
  • 2
  • 34
  • 53
2

Change result.saved-jobs to result["saved-jobs"].

IProblemFactory
  • 9,201
  • 8
  • 47
  • 65
2

You should use:

result["saved-jobs"]

Yves Lange
  • 3,844
  • 3
  • 20
  • 32
  • ah thanks, everyone gave the same answer but you were first by about 5 seconds :P will accept after 10 minutes is up – rpsep2 Jul 30 '14 at 13:31