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
Add a ment  | 

2 Answers 2

Reset to default 7
var 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