Answer the question
In order to leave comments, you need to log in
Why doesn't the recursive call work?
let a = {
name: "Marshall",
age: 47,
child: {
name: "Haley",
age: 24
}
};
function deepEqual(obj) {
let sort = Object.entries(obj);
sort = sort.sort()
obj = Object.fromEntries(sort);
for (let key in obj) {
if (typeof key === "object") {
deepEqual(key)
}
};
return obj;
};
console.log(deepEqual(a))
Answer the question
In order to leave comments, you need to log in
if (typeof key === "object") {
deepEqual(key)
}
if (typeof obj[key] === "object") {
deepEqual(obj[key])
}
Classical "plugging" in recursion.
The function deepEqual
returns a sorted object.
Where does the return value (nested object) go?
if (typeof key === "object") {
deepEqual(key)
}
if (typeof key === "object") {
obj[key] = deepEqual(key)
}
obj = Object.fromEntries(sort);
for (let key in obj) {
if (!obj.hasOwnProperty(key)) continue;
let subObj = obj[key];
if (typeof subObj === "object") {
obj[key] = deepEqual(subObj)
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question