Krisalay Krisalay - 3 months ago 8
Javascript Question

Get the values from JSON document

I have the following JSON document,

var data = [
{"id":1, "username":"user1"},
{"id":2, "username":"user2"},
{"id":3, "username":"user3"}
]


How to get all the values with the key "username", without using any loop?

Answer

You can make this in ES6/5

data.map(function(element) => {
  return element.username;
});

Documentation here

Or without loop

data[0].username
data[1].username
...