1
陣營狀態如下:檢查,如果所有屬性都是假的
[
{type: "Benzine", active: false},
{type: "Diesel", active: false},
{type: "Electricity", active: false}
]
我如何檢查是否所有active
值都是假的。
有沒有辦法用lodash做到這一點?
陣營狀態如下:檢查,如果所有屬性都是假的
[
{type: "Benzine", active: false},
{type: "Diesel", active: false},
{type: "Electricity", active: false}
]
我如何檢查是否所有active
值都是假的。
有沒有辦法用lodash做到這一點?
您可以使用下面的測試閹每active
屬性爲true:
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));
可以使用loadash的各項功能,以檢查是否活躍是爲每個對象錯誤。
var data = [
{type: "Benzine", active: false},
{type: "Diesel", active: false},
{type: "Electricity", active: true}
];
// First argument is the data and second argument is the predicate to check
var res = _.every(data, {active: false}); // Returns true if all elements pass the predicate match else false.
document.getElementById("data").innerHTML = res;
['!arr.some(obj => obj.active!== false);'](https://jsfiddle.net/5vxbdcy6/) – Tushar