简体   繁体   English

如果数组数组中的任何项为真或假,如何获取布尔值?

[英]How to get boolean value if any item in array of arrays is true or false?

I have this array, which is user generated and changes over time:我有这个数组,它是用户生成并随时间变化的:

const data = [["2020-09-14","15:00","60","Info","April Tucker","Other","yes"],
["2020-09-14","2:00","50","Text","April Tucker","Other","yes"]]

User picks a date which saves to sessionStorage (for example: 2020-11-21): sessionStorage.getItem('date')用户选择保存到 sessionStorage 的日期(例如:2020-11-21): sessionStorage.getItem('date')

Then I use forEach method to know if the first item of array item has the same value:然后我使用 forEach 方法来知道数组项的第一项是否具有相同的值:

 const checkForConflicts = () => {
    mydata.forEach((element) => {
      element.includes(sessionStorage.getItem('date'))
    })
  }

If I console log that I get boolean value for all item in the array.如果我控制台日志,我会得到数组中所有项目的布尔值。 How to return only one boolean value, true if checkForConflict returns only false values, or false value if it returned alt least one true value.如何只返回一个布尔值,如果 checkForConflict 只返回 false 值,则返回 true,如果返回至少一个 true 值,则返回 false 值。

It might be that one of either Array.prototype.every or Array.prototype.some or even both brings you the right tool at hand.可能是Array.prototype.everyArray.prototype.some或什至两者都为您带来了正确的工具。

... simplified example code ... ... 简化的示例代码 ...

 const data = [ ["2020-09-14","15:00","60","Info","April Tucker","Other","yes"], ["2020-09-14","2:00","50","Text","April Tucker","Other","yes"] ]; console.log( 'data.every(item => item[0] === "2020-09-14") ? ', data.every(item => item[0] === "2020-09-14") ); console.log( 'data.some(item => item[0] === "2020-09-14") ? ', data.some(item => item[0] === "2020-09-14") ); data.push( ["2020-09-15","2:00","50","Text","April Tucker","Other","yes"] ); console.log( 'data.every(item => item[0] === "2020-09-14") ? ', data.every(item => item[0] === "2020-09-14") ); console.log( 'data.some(item => item[0] === "2020-09-14") ? ', data.some(item => item[0] === "2020-09-14") );
 .as-console-wrapper { min-height: 100%!important; top: 0; }

Try the some() method:试试 some() 方法:

 const checkForConflicts = () => { mydata.forEach((element) => { element.some(!sessionStorage.getItem('date')) }) }

well you dont need use forEach and loop on the data you can just findIndex of conflicting item -1 mean their is no conflict好吧,您不需要使用 forEach 并在数据上循环,您只需找到冲突项的索引 -1 意味着它们没有冲突

const checkForConflicts = () => {
    return data.findIndex((element) => {
      element.includes(sessionStorage.getItem('date'))
    }) !== -1;
  }

Most importantly you have to return value in the function.最重要的是,您必须在函数中返回值。

I would suggest an array of objects, if not, alter this code to look at the first element in the array of arrays.我会建议一个对象数组,如果没有,请更改此代码以查看数组数组中的第一个元素。 Note I substituted a local for my test to show it here in a simple manner instead of the sessionStorage注意我用本地代替了我的测试,以简单的方式而不是sessionStorage在此处显示它

 const data = [{ date: "2020-09-14", time: "15:00", somenumber: "60", cheers: "Info", name: "April Tucker", "note": "Other", says: "yes" }, { date: "2020-09-14", time: "15:00", somenumber: "60", cheers: "Info", name: "Fred Tucker", "note": "Other", says: "yes" }, { date: "2020-09-13", time: "2:00", somenumber: "50", cheers: "Text", name: "April Tucker", "note": "Other", says: "yes" } ]; let testdate = "2020-09-14"; let testdate2 = "2020-09-19"; const checkForConflicts = (item, testme) => { // compare as dates to forgive formatting etc. return (new Date(item).getTime() === new Date(testme).getTime()) }; // I used .filter in case you need to know how many conflicts but .some() might work also let hasConflict = !!data.filter(x => checkForConflicts(x.date, testdate)).length; console.log(hasConflict); let conflictCount = data.filter(x => checkForConflicts(x.date, testdate)).length; console.log(conflictCount); let hasConflict2 = !!data.filter(x => checkForConflicts(x.date, testdate2)).length; console.log(hasConflict2);

暂无
暂无

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

相关问题 如何获取对象数组中布尔值的计数(如果为真) - How to get the count of boolean value (if it is true) in array of objects 如果 arrays 中的任何一个不为空,则将具有数组属性的 object 减少到 boolean 为真 - Reduce object with array properties to a boolean that is true if any of the arrays is not empty 如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回 boolean true 停止使用 Javascript 检查 - How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript 如何读取数组中所有函数的布尔值(真/假) - How to read boolean (true/false) of all functions in an array 如何从此语句返回布尔值true或false - How is the boolean value returned true or false from this statement 如何将我的 object boolean 值更新为真假? - How can i update my object boolean value to true and false? 如何在骨干模型属性中设置布尔值true / false - How to set boolean value true/false in to backbone model attribute 如何比较 2 个数组的相同值并将它们保存到带有 true 或 false 标志的第三个数组 - How to compare 2 arrays for same value and save them to 3rd array with true or false flags Boolean(“ false”)返回true。还有其他选择吗? - Boolean(“false”) returns true.. any alternative? 如何获取复选框的输入值(真/假) - How to get the input value of a checkbox (true/false)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM