snowberb – 14-24 Jun 22

I need to check for falsy or empty array props in an object, and I do not know how to make it so it checks all nested props. Any guidance?
S
Sniberb301d ago
I've done this for now but it won't work if the prop has a nested object with more props
export const objectPropsNotEmpty = (obj: Record<PropertyKey, unknown>) => {
for (const prop of Object.values(obj)) {
if (prop instanceof Array) {
if (!prop.length) {
return false;
}

continue;
}

if (isNil(prop)) {
return false;
}
}

return true;
};
export const objectPropsNotEmpty = (obj: Record<PropertyKey, unknown>) => {
for (const prop of Object.values(obj)) {
if (prop instanceof Array) {
if (!prop.length) {
return false;
}

continue;
}

if (isNil(prop)) {
return false;
}
}

return true;
};
recursion?
export const objectPropsNotEmpty = (obj: Record<PropertyKey, unknown>) => {
for (const prop of Object.values(obj)) {
if (typeof prop === 'object' && prop !== null) {
if (!objectPropsNotEmpty(prop)) {
return false;
}
}

if (prop instanceof Array) {
if (!prop.length) {
return false;
}

continue;
}

if (isNil(prop)) {
return false;
}
}

return true;
};
export const objectPropsNotEmpty = (obj: Record<PropertyKey, unknown>) => {
for (const prop of Object.values(obj)) {
if (typeof prop === 'object' && prop !== null) {
if (!objectPropsNotEmpty(prop)) {
return false;
}
}

if (prop instanceof Array) {
if (!prop.length) {
return false;
}

continue;
}

if (isNil(prop)) {
return false;
}
}

return true;
};
UU
Unknown User299d ago