Skip to content

Instantly share code, notes, and snippets.

@stevemu
Created April 12, 2019 11:09
Show Gist options
  • Save stevemu/1421a263c952907645279b8cdf00fab0 to your computer and use it in GitHub Desktop.
Save stevemu/1421a263c952907645279b8cdf00fab0 to your computer and use it in GitHub Desktop.
// convert a csv like array like this:
// const tourdates = [
// ["date", "venue", "city"],
// ["jan 1", "paladium", "worcester"],
// ["jan 2", "irving", "nyc"],
// ["jan 3", "gramercy", "nyc"]
// ];
// into objects, like this:
// objs = [ { date: "jan 1", venue: "paladium", city: "worcester" }, { date: "jan 2", venue: "irving", city: "nyc" }, ... ]
const tourdates = [
["date", "venue", "city"],
["jan 1", "paladium", "worcester"],
["jan 2", "irving", "nyc"],
["jan 3", "gramercy", "nyc"]
];
let titles = tourdates[0];
let dates = tourdates.slice(1, tourdates.length);
let objs = dates.map((item) => {
let obj = {};
for (let i = 0; i < titles.length; i++) {
obj[titles[i]] = item[i];
}
return obj;
})
console.log(objs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment