Last active
March 8, 2022 00:31
-
-
Save n-a-t-e/e00947beb1a67424816ac613850bbe1f to your computer and use it in GitHub Desktop.
Unnesting response from https://www.marinespecies.org/rest/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data = { | |
AphiaID: 1, | |
rank: "Superdomain", | |
scientificname: "Biota", | |
child: { | |
AphiaID: 2, | |
rank: "Kingdom", | |
scientificname: "Animalia", | |
child: { | |
AphiaID: 51, | |
rank: "Phylum", | |
scientificname: "Mollusca", | |
child: { | |
AphiaID: 101, | |
rank: "Class", | |
scientificname: "Gastropoda", | |
child: { | |
AphiaID: 156485, | |
rank: "Subclass", | |
scientificname: "Vetigastropoda", | |
child: { | |
AphiaID: 1052448, | |
rank: "Order", | |
scientificname: "Trochida", | |
child: { | |
AphiaID: 156489, | |
rank: "Superfamily", | |
scientificname: "Trochoidea", | |
child: { | |
AphiaID: 382180, | |
rank: "Family", | |
scientificname: "Calliostomatidae", | |
child: { | |
AphiaID: 411630, | |
rank: "Subfamily", | |
scientificname: "Calliostomatinae", | |
child: { | |
AphiaID: 138584, | |
rank: "Genus", | |
scientificname: "Calliostoma", | |
child: null, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}; | |
function unnestWormResponse(aphia, accumulator = {}) { | |
if (!aphia) return; | |
accumulator[aphia.rank] = aphia.scientificname; | |
unnestWormResponse(aphia.child, accumulator); | |
return accumulator; | |
} | |
const result = unnestWormResponse(data); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment