admin管理员组文章数量:1278787
I am trying to extract all the values from key-value pair of JSON message and then concatenate. I am trying with below code snippet after referring few other questions on same forum. It is working and first Key is printing, but for value, it is giving [object Object] i.e., It is not able to parse the data under "Employees" key.
How can I extract all the values and concatenate in a single string in Node JS. like employname String = John Doe|Anna Smith|PeterJones.
The mented code is the second way, I tried to extract values, but it's not working either.
Thanks.
var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);
console.log(typeof obj);
for (var key in obj) {
console.log(' name=' + key + ' value=' + obj[key]);
}
/*
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var obj1 = obj[key];
console.log(obj[key]);
for (var prop in obj1) {
if(obj1.hasOwnProperty(prop))
{
console.log(obj[prop]);
}
}
}
}
*/
I am trying to extract all the values from key-value pair of JSON message and then concatenate. I am trying with below code snippet after referring few other questions on same forum. It is working and first Key is printing, but for value, it is giving [object Object] i.e., It is not able to parse the data under "Employees" key.
How can I extract all the values and concatenate in a single string in Node JS. like employname String = John Doe|Anna Smith|PeterJones.
The mented code is the second way, I tried to extract values, but it's not working either.
Thanks.
var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);
console.log(typeof obj);
for (var key in obj) {
console.log(' name=' + key + ' value=' + obj[key]);
}
/*
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var obj1 = obj[key];
console.log(obj[key]);
for (var prop in obj1) {
if(obj1.hasOwnProperty(prop))
{
console.log(obj[prop]);
}
}
}
}
*/
Share
Improve this question
edited Dec 7, 2015 at 20:14
Jordan Running
106k18 gold badges188 silver badges187 bronze badges
asked Dec 7, 2015 at 20:09
ManoMano
1431 gold badge2 silver badges13 bronze badges
1
-
It seems to be working, just that the console is displaying that it's an object. With
console.log(' name,value', key, obj[key]);
it displays correctly parsed object. – Pandaiolo Commented Dec 7, 2015 at 20:18
2 Answers
Reset to default 7var obj = {
employees: [
{ firstName: "John",
lastName: "Doe"
},
{ firstName: "Anna",
lastName: "Smith"
},
{ firstName: "Peter",
lastName: "Jones"
}
]
}
var names = obj.employees.map(function (employee) {
return employee.firstName + ' ' + employee.lastName;
});
console.log(names);
// => [ "John Doe", "Anna Smith", "Peter Jones" ]
If you really just want to concatenate all values without regard to order, you can do this:
var concatenatedValues = obj.employees.map(function (employee) {
return Object.keys(employee).map(function(key) {
return employee[key];
}).join(' ');
});
...but be aware that you might end up with "Doe John"
etc.
var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);
console.log(typeof obj);
for (var key in obj.employees) {
console.log(' name=' + obj.employees[key].firstName + ' value=' + obj.employees[key].lastName);
};
本文标签: javascriptExtracting value from keyvalue of Object in NodejsStack Overflow
版权声明:本文标题:javascript - Extracting value from key-value of Object in Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741237096a2363233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论