Skip to content

Instantly share code, notes, and snippets.

@the-glima
Last active May 6, 2022 17:41
Show Gist options
  • Save the-glima/f6e294682bb052585fa637e039f2302c to your computer and use it in GitHub Desktop.
Save the-glima/f6e294682bb052585fa637e039f2302c to your computer and use it in GitHub Desktop.
NexHealth: Front-end Challenge JS Part A
const URL = "https://run.mocky.io/v3/c4b1255a-2251-42fd-adbf-bc92f9a391e7";
// Get the data
const getData = async () =>
fetch(URL)
.then((response) => response.json())
.then((data) => data)
.catch((error) => console.log(error));
// Part A Solution
export const filterDupes = async (shouldPrint = true) => {
const data = await getData();
if (!data) {
console.log("No data found", data);
return;
}
const configDupes = {};
// You could also use a for loop here,
// but more modern forEach syntax is preferable and easier to read.
data.forEach((config) => {
// Or any suitable variation
let key = `${config["inst_id"]}_${config["loc_id"]}`;
if (configDupes[key] === undefined) {
configDupes[key] = [config];
} else {
configDupes[key].push(config);
}
});
if (shouldPrint) {
console.log("Part A: Output");
print(configDupes);
}
return configDupes;
};
// Init
filterDupes();
// Optional pretty print the values
const print = (data) => {
for (const key in data) {
let values = data[key];
let configIds = values.map((config) => config["id"]);
console.log(`${key} => ${configIds}`);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment