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]? 
1
  • Doesn't seem to be converting object to an array. This looks like a question of how to parse json and then how to map that json to pluck a field from all the objects Commented Feb 24, 2017 at 18:18

1 Answer 1

4

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);

Sign up to request clarification or add additional context in comments.

1 Comment

You sir, are wonderful. That is exactly what I was after! Thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.