admin管理员组

文章数量:1287606

I have following JSON:

  [ {
    "id":1,
    "firstName":"Markus",
    "lastName":"Maier",
    "email":"[email protected]",
    "externalId":"mmaie",
    "pany":"Intel"
    },
    {
    "id":2,
    "firstName":"Birgit",
    "lastName":"Bauer",
    "email":"[email protected]",
    "externalId":"bbaue"
    } ]

I want to iterate through both objects and get the value of the "email" key.. what is the simplest way to do that? Thanks!

I have following JSON:

  [ {
    "id":1,
    "firstName":"Markus",
    "lastName":"Maier",
    "email":"[email protected]",
    "externalId":"mmaie",
    "pany":"Intel"
    },
    {
    "id":2,
    "firstName":"Birgit",
    "lastName":"Bauer",
    "email":"[email protected]",
    "externalId":"bbaue"
    } ]

I want to iterate through both objects and get the value of the "email" key.. what is the simplest way to do that? Thanks!

Share Improve this question edited Jan 23, 2017 at 13:02 MarkusFsx asked Jan 23, 2017 at 12:56 MarkusFsxMarkusFsx 531 silver badge5 bronze badges 5
  • 1 This isn't even valid javascript. A object can't have values without keys (the inner objects). Should the outer braces be array braces? – Michael Ritter Commented Jan 23, 2017 at 12:59
  • What have you tried? What are expected results? This isn't a free code writing service and you are expected to have done some research and show your attempts – charlietfl Commented Jan 23, 2017 at 13:01
  • @ Michael Yes they should, i'm sorry! Edited now.. @charlietfl I'm very new to coding and i don't really have an idea where to start – MarkusFsx Commented Jan 23, 2017 at 13:03
  • Start by studying some tutorials then – charlietfl Commented Jan 23, 2017 at 13:04
  • @MarkusFsx see the code below. – Ataur Rahman Munna Commented Jan 23, 2017 at 13:05
Add a ment  | 

2 Answers 2

Reset to default 11

If you want to end up with an array of just the emails, you may want to look into the .map() function.

var data = [{
  "id": 1,
  "firstName": "Markus",
  "lastName": "Maier",
  "email": "[email protected]",
  "externalId": "mmaie",
  "pany": "Intel"
}, {
  "id": 2,
  "firstName": "Birgit",
  "lastName": "Bauer",
  "email": "[email protected]",
  "externalId": "bbaue"
}];

var emails = data.map(d => d.email);

console.log(emails);

Follow the code.loop through the data each object and for each object get the desired value by key.

var data = [ {
    "id":1,
    "firstName":"Markus",
    "lastName":"Maier",
    "email":"[email protected]",
    "externalId":"mmaie",
    "pany":"Intel"
    },
    {
    "id":2,
    "firstName":"Birgit",
    "lastName":"Bauer",
    "email":"[email protected]",
    "externalId":"bbaue"
    } ];
    
    for(var i=0; i< data.length;i++){
        console.log(data[i]['email']);
    }

本文标签: JavaScriptIterate through JSON Object and get all the values for a specific keyStack Overflow