简体   繁体   English

有没有办法检查数组中的虚假值?

[英]Is there a way to check for falsy values from an array?

I want to check a bunch of variables for falsy values.我想检查一堆变量的假值。

Instead of writing a very long if statement, eg:而不是写一个很长的 if 语句,例如:

if (!userData.email || !userData.lastName || ! userData.firstName etc...

I wanted to put these values in an array and simply check the array, eg:我想将这些值放在一个数组中并简单地检查数组,例如:

      var detailsConditionsArr = [
        userData.firstName,
        userData.lastName,
        userData.email,
        userData.phone,
        userData.address,
        userData.mailAddress,
      ]

      if (detailsConditionsArr.indexOf(false) === 1) {
        console.log("Found a falsy");
      }

But the above doesn't work (if statement never fires).但以上不起作用(如果语句永远不会触发)。

The values could be false , null or '' .值可以是falsenull''

Is there a way to check these from the array?有没有办法从数组中检查这些?

Ideal use case for Array#some Array#some 的理想用例

Check for falsy value in the iteration, if the test succeeds, true will be returned.在迭代中检查 falsy 值,如果测试成功,则返回true

 var A = ["Name", ""]; var B = ["Name", "30"]; var C = ["Name", "30", null]; if (A.some(el => !el)) { console.log("Found a falsy in A"); } if (B.some(el => !el)) { console.log("Found a falsy in B"); } if (C.some(el => !el)) { console.log("Found a falsy in C"); }

You can use the key names you want to check as an array, then do the check like this:您可以使用要检查的键名作为数组,然后像这样进行检查:

 const userData = { email: '', lastName: '', firstName: '' }; const hasFalsy = ['email', 'lastName', 'firstName'].some(k => !Boolean(userData[k])); if (hasFalsy) { console.log('found a falsy'); }

You can use reduce to return a single value from the array like so:您可以使用 reduce 从数组中返回单个值,如下所示:

var detailsConditionsArr = [
    userData.firstName,
    userData.lastName,
    userData.email,
    userData.phone,
    userData.address,
    userData.mailAddress,
  ]

if (detailsConditionsArr.reduce((carry, el)=> el ? carry : true, false)) {
  console.log("Found a falsy");
}

You can use some method.你可以使用some方法。

 var detailsConditionsArr = [ "test", 12, false, "s", "sd", "test", ] if (detailsConditionsArr.some(x => !x)) { console.log("Found a falsy"); } // or //if (detailsConditionsArr.some(x => !Boolean(x))) { // console.log("Found a falsy"); //}

You can loop the list of properties and then check if any is falsy :您可以循环属性列表,然后检查是否有任何错误:

 const userData = { firstName : 'John', lastName : 'Doe', email : null, phone : 42, address : 'Hello', mailAddress : 'world' }; function HasObjectAnyFalsyValues(obj) { for (let prop in obj) { if (Object.prototype.hasOwnProperty.call(obj, prop) && !obj[prop]) { // <-- check for falsy return true; } } return false; } console.log('The object has ' + (!HasObjectAnyFalsyValues(userData) ? 'no ' : '') + 'falsy values');

This will check all properties .这将检查所有属性 If you want to list specific properties instead, use one of the other answers posted如果您想列出特定的属性,请使用发布的其他答案之一

Using some is a clean way for doing this job.使用some是完成这项工作的一种干净的方式。

The Array.prototype.some() method tests whether at least one element in the array passes the test implemented by the provided function (in your case, falsy value). Array.prototype.some()方法测试数组中是否至少有一个元素通过了由提供的函数实现的测试(在您的情况下为假值)。

 const userData = {}; userData.firstName = "dd"; userData.lastName = false; userData.email = 0; userData.phone = 123; userData.address = "aad"; userData.mailAddress = "dff"; const detailsConditionsArr = [ userData.firstName, userData.lastName, userData.email, userData.phone, userData.address, userData.mailAddress, ] const detailsConditionsArr2 = [true, 1, "fff"]; const doesHaveFalsy = obj => obj.some(x => !x); if (doesHaveFalsy(detailsConditionsArr)) { console.log("detailsConditionsArr Found a falsy"); } if (doesHaveFalsy(detailsConditionsArr2)) { console.log("detailsConditionsArr2 Found a falsy"); }

More about some - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some更多关于一些 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM