gpt4 book ai didi

javascript - 检查所有属性是否为假

转载 作者:行者123 更新时间:2023-11-30 11:50:44 26 4
gpt4 key购买 nike

react 状态如下:

[
{type: "Benzine", active: false},
{type: "Diesel", active: false},
{type: "Electricity", active: false}
]

如何检查所有 active 值是否为 false。

有没有办法用 lodash 做到这一点?

最佳答案

您可以使用以下方法测试每个 active 属性是否为真:

    var arr = [
{type: "Benzine", active: false},
{type: "Diesel", active: false},
{type: "Electricity", active: false}
]

console.log(arr.every(obj => obj.active));

var arr = [
{type: "Benzine", active: true},
{type: "Diesel", active: true},
{type: "Electricity", active: true}
]

console.log(arr.every(obj => obj.active));

    var arr = [
{type: "Benzine", active: false},
{type: "Diesel", active: true},
{type: "Electricity", active: false}
]

console.log(arr.every(obj => obj.active));

关于javascript - 检查所有属性是否为假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39567801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com