1

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')

Then I use forEach method to know if the first item of array item has the same value:

 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.

2
  • "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." - but checkForConflict doesn't return anything... Commented Sep 10, 2020 at 18:50
  • Please have a look into both Array.prototype.every and Array.prototype.some. This should enable you to pick the right tool(s). Commented Sep 10, 2020 at 19:52

4 Answers 4

1

It might be that one of either Array.prototype.every or Array.prototype.some or even both brings you the right tool at hand.

... 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; }

Sign up to request clarification or add additional context in comments.

Comments

0

Try the some() method:

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

Comments

0

well you dont need use forEach and loop on the data you can just findIndex of conflicting item -1 mean their is no conflict

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

Most importantly you have to return value in the function.

Comments

0

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

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.