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
2 Answers
Reset to default 11If 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
版权声明:本文标题:JavaScript | Iterate through JSON Object and get all the values for a specific key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741264730a2368245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论