Skip to content Skip to sidebar Skip to footer

Extract Fields From Object

If I have this: var myObj = [ { name: 'A', number: 'b1',level: 0 }, { name: 'B', number: 'b2',level: 0 }, ]; How can I extract all the names like: 'names': { 'A', 'B' }

Solution 1:

You could use this function to get an array of values, not object properties (which are not suited for that):

functiongetColumn(arr, column) {
    return arr.map(function (rec) { return rec[column] });
}

// Sample datavar myObj = [
  { name: 'A', number: 'b1',level: 0 },
  { name: 'B', number: 'b2',level: 0 },
];

// Get namesvar names = getColumn(myObj, 'name');

// Outputconsole.log(names);

Post a Comment for "Extract Fields From Object"