Skip to content

Instantly share code, notes, and snippets.

@eddieajau
Created August 12, 2014 23:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eddieajau/5f3e289967de60cf7bf9 to your computer and use it in GitHub Desktop.
Save eddieajau/5f3e289967de60cf7bf9 to your computer and use it in GitHub Desktop.
Extract a column from an array of JavaScript objects.
function extractColumn(arr, column) {
function reduction(previousValue, currentValue) {
previousValue.push(currentValue[column]);
return previousValue;
}
return arr.reduce(reduction, []);
}
@Enelar
Copy link

Enelar commented Oct 9, 2018

function extractColumn(arr, column) {
  return arr.map(x => x[column])
}

@f4ww4z
Copy link

f4ww4z commented Nov 5, 2019

@Enelar thanks

@pauloffborba
Copy link

pauloffborba commented Jun 15, 2020

function extractColumn(arr, column) {
  return arr.map(x => x[column])
}

ES6 way :)
let extractColumn = (arr, column) => arr.map(x=>x[column]);

@Enelar
Copy link

Enelar commented Jun 16, 2020

@pauloffborba const :P

@jdbdline
Copy link

jdbdline commented Nov 5, 2021

Awesome function Eddie, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment