I receive multiple JSONs from an API (17 API calls with Promise.all()
). For example
[
{
key: value,
key: value,
key: value,
key: value,
values: [
{
key: value,
key: value,
key: value,
keyIWant: value
}
{
key: value,
key: value,
key: value,
keyIWant: value
}
{
key: value,
key: value,
key: value,
keyIWant: value
}
]
}
{
key: value,
key: value,
key: value,
key: value,
values: [
{
key: value,
key: value,
key: value,
keyIWant: value
}
{
key: value,
key: value,
key: value,
keyIWant: value
}
{
key: value,
key: value,
key: value,
keyIWant: value
}
]
}
{
key: value,
key: value,
key: value,
key: value,
values: [
{
key: value,
key: value,
key: value,
keyIWant: value
}
{
key: value,
key: value,
key: value,
keyIWant: value
}
{
key: value,
key: value,
key: value,
keyIWant: value
}
]
}
]
and I really have no idea to get the key I want (and to get it on a more generic approach). So far my attempts have been
static _findKey(nestedData) {
const result = [];
const buffer = [];
for (const prop in nestedData) {
const value = nestedData[prop];
if (typeof value === "object") {
buffer.push(Class._findKey(value));
}
if (prop === "keyIWant") { // key would be an argument from the function if it'd worked
result.push(value); // doesn't work because of recursive call?
}
}
return result;
}
and
static _findKey(projects) { //
return projects.forEach(project => {
return project.values.forEach(projectValue => {
return projectValue.key;
});
});
}
Do you have some more ideas? I'm still learning JavaScript and thus want a clean and comprehensive solution, but couldn't build one by myself.
See Question&Answers more detail:
os