Skip to content

Instantly share code, notes, and snippets.

@awong-dev
Last active March 20, 2020 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awong-dev/43c88318b95da1d661de062e971c8b3c to your computer and use it in GitHub Desktop.
Save awong-dev/43c88318b95da1d661de062e971c8b3c to your computer and use it in GitHub Desktop.
Covert sheets flat-row format into object map.
function toDataByLocation(data) {
const headers = data.values[0];
const approvedIndex = headers.findIndex( e => e === 'Approved' );
const stateIndex = headers.findIndex( e => e === 'State?' );
const cityIndex = headers.findIndex( e => e === 'City' );
const data_by_location = {};
const published_entries = data.values.slice(1).filter((entry) => entry[approvedIndex] === "x");
published_entries.forEach( entry => {
const state = entry[stateIndex];
const city = entry[cityIndex];
let entry_array;
if (!(state in data_by_location) || !(city in data_by_location[state])) {
entry_array = [];
if (state in data_by_location) {
data_by_location[state][city] = entry_array;
} else {
data_by_location[state] = { [city]: entry_array };
}
} else {
entry_array = data_by_location[state][city];
}
const entry_obj = {};
headers.forEach( (value, index) => {
if (entry[index] !== undefined) {
entry_obj[value] = entry[index]
} else {
entry_obj[value] = ""
}
});
entry_array.push(entry_obj);
});
return data_by_location;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment