-1

I've json response like this, I want to get all project names from this.

I have done as usual as iterate through arrays but here "d" is not an array. How can I do to get result:

{
    "d": {
    "results": [
        {
            "ProjectId": "696fcc7c-f355-e511-93f0-00155d008500",
            "ProjectName": "Payroll",
            "EnterpriseProjectTypeDescription": null,
            "EnterpriseProjectTypeId": null,
            "EnterpriseProjectTypeIsDefault": null
        },
        {
            "ProjectId": "696fcc7c-f355-e511-93f0-00155d008505",
            "ProjectName": "Permanant",
            "EnterpriseProjectTypeDescription": null,
            "EnterpriseProjectTypeId": null,
            "EnterpriseProjectTypeIsDefault": null
        }
    ]
    }
}
1
  • 2
    iterate through obj.d.results to get your different projects. Commented Sep 29, 2015 at 6:02

2 Answers 2

3

Use Array.prototype.map():

var names = myData.d.results.map(function(item){
    return item.ProjectName;
});

This will result in an array like:

["Payroll", "Permanant"]

(Assuming myData is the object in your question)

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

4 Comments

What's with the downvote? What's wrong with this answer?
I didn't downvote you, but I'm assuming it's due to Array.prototype.map first being introduced in IE9. A lot of business applications are reliant on a minimum of IE8 (which even Google will drop support for in a month.. then again, the MDN link has a polyfill, so it shouldn't be an issue).
If that is the reason, then I feel sorry for the person that has to support IE8 ;-)
Tell me about it! Hopefully Microsoft's "update everything to Windows 10" will get rid of the XP installations that can't upgrade to IE9. To think that there are still businesses relying on a 14 year old OS to function is beyond me!
1

You can use jsonObject.d.results to get the array from response and use forEach() to iterate array

var res = {
  "d": {
    "results": [{
        "ProjectId": "696fcc7c-f355-e511-93f0-00155d008500",
        "ProjectName": "Payroll",
        "EnterpriseProjectTypeDescription": null,
        "EnterpriseProjectTypeId": null,
        "EnterpriseProjectTypeIsDefault": null
      }, {
        "ProjectId": "696fcc7c-f355-e511-93f0-00155d008505",
        "ProjectName": "Permanant",
        "EnterpriseProjectTypeDescription": null,
        "EnterpriseProjectTypeId": null,
        "EnterpriseProjectTypeIsDefault": null
      }
    ]
  }
};

res.d.results.forEach(function(v) {
  document.write(v.ProjectName + '<br>')
})

If you want to get it as an array then you can use map()

var res = {
  "d": {
    "results": [{
      "ProjectId": "696fcc7c-f355-e511-93f0-00155d008500",
      "ProjectName": "Payroll",
      "EnterpriseProjectTypeDescription": null,
      "EnterpriseProjectTypeId": null,
      "EnterpriseProjectTypeIsDefault": null
    }, {
      "ProjectId": "696fcc7c-f355-e511-93f0-00155d008505",
      "ProjectName": "Permanant",
      "EnterpriseProjectTypeDescription": null,
      "EnterpriseProjectTypeId": null,
      "EnterpriseProjectTypeIsDefault": null
    }]
  }
};

var result = res.d.results.map(function(v) {
  return v.ProjectName;
})

document.write(JSON.stringify(result));

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.