Sniberb
Sniberb14mo ago

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?
3 Replies
Sniberb
Sniberb14mo 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;
};
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
reactibot
reactibot14mo ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1121445495940841532