I saw this block of code here How to count number of values in array?. As a solution for my other question.
This solves my issue but I do not want to use it without knowing what it means. Can someone give me a detailed explanation of this code?
function count(object, key, subKey) {
const noObject = o => !o || typeof o !== 'object';
function subCount(object) {
if (noObject(object)) return 0;
if (subKey in object) return 1;
return Object.values(object).reduce((s, o) => s + subCount(o), 0);
}
if (noObject(object)) return 0;
if (key in object) return subCount(object[key]);
return Object.values(object).reduce((s, o) => s + count(o, key, subKey), 0);
}
Is this used to count the number of values in a JSON array?