how to check if an object has at least one true va

2020-01-20 05:29发布

I have an array that looks like following

values = {de: true, en: false, nl: false, pl: false, ru: false}

I using the array to make a layout change in jsx, how can I check if the array has at least one true value in JSX,

any help would be appreciated.

1条回答
闹够了就滚
2楼-- · 2020-01-20 06:16

Assuming that values is actually an object, check if .some of the Object.values of the object are true:

const values = {de: true, en: false, nl: false, pl: false, ru: false};

const someTruthy = Object.values(values).some(val => val === true);
console.log(someTruthy);

(if the only truthy value is true, you can use (val => val) instead)

查看更多
登录 后发表回答