I have borrowed travese
from another SO post
function traverse(o,func) {
for (var i in o) {
func.apply(this,[i,o[i]]);
if (o[i] !== null && typeof(o[i])=="object") {
//going one step down in the object tree!!
traverse(o[i],func);
}
}
}
So now we can do this:
const data = {
name: 'Alex',
lastName: '',
age: 24,
lang: { lang1: 'fr', lang2: 'en' },
courses: { c1: '', c2: 'math', c3: '' },
books: { book1: '', book2: 'book2' },
};
function getCount(obj) {
// Determine if a value is 'filled'
let isFilled = (value) => value !== '' && typeof value !== 'object'
let count = 0
traverse(data, (key, value) => {
if (isFilled(value))
count++
})
return count
}
let result = getCount(data)