Skip to content

Instantly share code, notes, and snippets.

@awong-dev
Last active March 20, 2020 23:22
Show Gist options
  • Select an option

  • Save awong-dev/43c88318b95da1d661de062e971c8b3c to your computer and use it in GitHub Desktop.

Select an option

Save awong-dev/43c88318b95da1d661de062e971c8b3c to your computer and use it in GitHub Desktop.

Revisions

  1. awong-dev revised this gist Mar 20, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ function toDataByLocation(data) {
    }
    const entry_obj = {};
    headers.forEach( (value, index) => {
    if (entry_obj[index] !== undefined) {
    if (entry[index] !== undefined) {
    entry_obj[value] = entry[index]
    } else {
    entry_obj[value] = ""
  2. awong-dev revised this gist Mar 20, 2020. No changes.
  3. awong-dev created this gist Mar 20, 2020.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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_obj[index] !== undefined) {
    entry_obj[value] = entry[index]
    } else {
    entry_obj[value] = ""
    }
    });
    entry_array.push(entry_obj);
    });

    return data_by_location;
    }