1

let result = []
I have array const arr = ["A", "B"]
and array of array const arrOfArr = [["A","C"],["A","B"],["B","C"]]
I want find arr in arrOfArr, if exist will push "Pass" to result my code:

 arrOfArr.forEach((el) => {
        if (JSON.stringify(arr) === JSON.stringify(el)) {
          arrResult.push("Pass");
        } else {
          arrResult.push("False");
        }
      });

but If condition is not true will push many False to result
What solution for me if condition is not true just push 1 False to result?

  • Why all the `stringify` stuff? That can be quite wasteful in terms of garbage produced. – tadman Nov 24 '20 at 09:00
  • You can use [some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) – jarmod Nov 24 '20 at 09:01
  • The linked answer is terrible. Don't use that, outdated af – Cristian Traìna Nov 24 '20 at 09:14
  • JSON.stringify has been demonstrated to be faster than any other solution. No garbage produced. If you want only a result you can just use `arrResult.push(arrOfArr.some(...) ? 'Pass' : 'False')` – Cristian Traìna Nov 24 '20 at 09:19

0 Answers0