0

my server upon request, serves JSON from a node mysql query, but only the names row. Example:

{
"Name": "Charles"
}, etc. 

How can I, get the value of Name and put it into an array? say I have this,

  [
 {
  "Name": "Charles"
 },
 {
  "Name": "Alex"
 }
  ]

how can I get, Charles and Alex, into an array?

Like:

Names = ["Charles", "Alex]? 
Tom
  • 73
  • 1
  • 9
  • Doesn't seem to be converting object to an array. This looks like a question of [how to parse json](http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) and then how to map that json to [pluck a field](http://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) from all the objects – Atreys Feb 24 '17 at 18:18

1 Answers1

5

You can make use of the map function. The map method creates a new array with the results of calling a provided function on every element in this array. In your case, you need to select only the Name.

var arr = [
 {
  "Name": "Charles"
 },
 {
  "Name": "Alex"
 }];
  
var names = arr.map(x=>x.Name)
console.log(names);
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41