Skip to content

Instantly share code, notes, and snippets.

@beall49
Last active November 8, 2018 21:38
Show Gist options
  • Select an option

  • Save beall49/761991f9c8f04a2b6ed10e8231e783ca to your computer and use it in GitHub Desktop.

Select an option

Save beall49/761991f9c8f04a2b6ed10e8231e783ca to your computer and use it in GitHub Desktop.
/** building.json
{
"results" = [ {
"attributes": {
"BUILDING": "B041",
.....
}
},
{
"attributes": {
"BUILDING": "B071",
....
},
{
"attributes": {
"BUILDING": "B111",
....
}
},
{
"attributes": {
"BUILDING": "B111"
.....
}
}]
}
*/
const buildingJson = require('./building');
const buildingObject = (id) => {
return {
id: id,
idNumber: id.substring(1),
};
};
const reduceBuildings = (buildings) => {
return buildings.reduce((groups, building) => {
const idNumber = building.idNumber;
const id = building.id;
const group = groups.find(g => g.idNumber === idNumber);
if (group) {
return groups;
}
groups.push({
id: id,
idNumber: idNumber,
});
return groups;
}, []);
};
const buildings = (buildingJson.results.map(b => buildingObject(b.attributes.BUILDING)));
const buildingNumbers = reduceBuildings(buildings);
console.log(JSON.stringify(buildingNumbers));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment