Why doesn't a recursive call?

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))

Why doesn't a recursive sorting? The object is sorted ,and its property object no. What's the matter please tell me.
PEOPLE POJALUSTA HELP OR I'LL GO TO SLEEP NOW
April 7th 20 at 10:41
2 answers
April 7th 20 at 10:43
Solution
if (typeof key === "object") {
deepEqual(key)
 }


Replace

if (typeof obj[key] === "object") {
deepEqual(obj[key])
 }


https://developer.mozilla.org/uk/docs/Web/JavaScri...
It wouldn't work :-/ - rickey_Walsh6 commented on April 7th 20 at 10:46
@rickey_Walsh6,
if (typeof obj[key] === "object") {
 obj[key] = deepEqual(obj[key])
 }


And so? - Seamus.Price59 commented on April 7th 20 at 10:49
sort = sort.sort()
; put at the end, better don't leave without it, even if JS allows you to do this.
When write the code, you will stumble on this feature. - ahmad_Lebsack26 commented on April 7th 20 at 10:52
April 7th 20 at 10:45
Classic "plugging" in the recursion.

Function deepEqual returns the sorted object.
And here is where does the return value (nested object)?
if (typeof key === "object") {
deepEqual(key)
 }


Nowhere. It is sorted into the following levels of recursion, but will not be refunded.

I guess should be like this:

if (typeof key === "object") {
 obj[key] = deepEqual(key)
 }


UPD
And in General, for..in iterates through object keys and not their values!
Offhand:
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)
}
};
I will add that judging by the question, the author is very far from understanding the question "what is the data in JS, how they are stored and how to change them?" - asha_White40 commented on April 7th 20 at 10:48
@asha_White40definitely noticed)
But in my defense, I'm about 3 months engaged and in school. - rickey_Walsh6 commented on April 7th 20 at 10:51

Find more questions by tags JavaScript