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
  • 3,061
  • 10
  • 39
  • 52

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
  • 11,835
  • 2
  • 35
  • 53
2

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

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
2

You should use:

result["saved-jobs"]

Yves Lange
  • 3,914
  • 3
  • 21
  • 33
  • 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