score:1

Accepted answer

Simple javascript is sufficient; d3 doesn't really add any ease to doing this particular task. You'll see array.map() often in d3 code as it does the job you are looking for:

var data = [
  {name: "Tim", grades: [2.0, 2.2, 1.7]},
  {name: "Mike", grades: [1.7, 1.3, 2.7]},
  {name: "Max", grades: [3.0, 2.0, 2.3]},
  {name: "Peter", grades: [1.5, 2.0, 1.5]}
];
    
var names = [];

names = data.map(function(d) { return d.name; })
    
console.log(names);

Here is a description of the array.map() method:

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

If you really want to use d3 to achieve the same effect, you could use d3.map() (see this answer and the API documentation). You can use the .keys() method of a d3.map() to get a list of the keys:

var data = [
  {name: "Tim", grades: [2.0, 2.2, 1.7]},
  {name: "Mike", grades: [1.7, 1.3, 2.7]},
  {name: "Max", grades: [3.0, 2.0, 2.3]},
  {name: "Peter", grades: [1.5, 2.0, 1.5]}
];

console.log(d3.map(data, function(d) { return d.name}).keys());
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.min.js"></script>


Related Query

More Query from same tag