[英]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.every
或Array.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.