How to get a list of key values from array of JavaScript objects [duplicate]

You can take Array.map(). This method returns an array with the elements from the callback returned. It expect that all elements return something. If not set, undefined will be returned.

var students = [{
    name: 'Nick',
    achievements: 158,
    points: 14730
}, {
    name: 'Jordan',
    achievements: '175',
    points: '16375'
}, {
    name: 'Ramon',
    achievements: '55',
    points: '2025'
}];
var nameArray = students.map(function (el) { return el.name; });
document.getElementById('out').innerHTML = JSON.stringify(nameArray, null, 4);
<pre id="out"></pre>

Leave a Comment

tech