2

I have got this code:

var test = [[1,1], "b","a"];
    function findArray(element) {
        return element == [1,1];
    }
    console.log(test.find(findArray));

It returns:

undefined

What should I do to find the [1,1] array inside of the test array? (I do not want to loop through it though)

3
  • 1
    I do not want to loop through it though- No other way. You cannot directly compare arrays Commented Oct 2, 2019 at 18:01
  • You could do return JSON.stringify(element) == JSON.stringify([1, 1]);. I don't really like that solution myself though. Commented Oct 2, 2019 at 18:13
  • Possible duplicate of How to compare arrays in JavaScript? Commented Oct 2, 2019 at 18:23

2 Answers 2

6

You can't compare two objects with === or == since they are references and will evaluate to true only if they are pointing to the same address.

You need to match each element from first array with respective index in second array to check for similarity, you can use every.

var test = [ [1, 1], "b", "a"];

function findArray(element) {
  if(Array.isArray(element)){
    let arrayToMatchWith = [1,1]
    if(element.length === arrayToMatchWith.length){
      return element.every((v,i)=> v === arrayToMatchWith[i])
    }
  }
  return false
}
console.log(test.find(findArray));
console.log([[1,2]].find(findArray));
console.log([[1,1,1]].find(findArray));
console.log([[1]].find(findArray));


Could I pass the searched array as an argument?

Yes you can. Here I am using a curried function:

var test = [[1, 1], "b", "a"];

let curried = (arr) => (element) => {
  if (Array.isArray(element)) {
    if (element.length === arr.length) {
      return element.every((v, i) => v === arr[i])
    }
  }
  return false
}

let curried1 = curried([1,1])
console.log(test.find(curried1));
let curried2 = curried([1,2,2])
console.log([[1, 2]].find(curried2));
let curried3 = curried([1,1,1])
console.log([[1, 1, 1]].find(curried3));
let curried4 = curried([1])
console.log([[1]].find(curried4));

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

5 Comments

"you can't compare two references", maybe reword that phrase since that is exactly what == and === do. They check (in the case of objects) if they reference the same "address"
@CodeManiac could I pass the searched array as an argument?
@CodeManiac whe I try to add an argument to findArray I get: "Uncaught TypeError: Cannot read property 'length' of undefined
@JacekWikiera yes you can, see the second snippet, since value being binded to function by callback of find so you don't have much control over the value being passed to the function you can use currying
“You need to match each element with respective […]” — that sentence is not complete. What do you mean here?
0

The array literal in your comparison is a different object than the array inside your original array. If you derive a comparable entity from each array, you can check for that inside the find method. One way to do it is by calling the toString method.

This is only required if you really don't want to loop through the array for comparison.

var test = [[1,1], "b","a"];

 function findArray(element) {
    
   if (element.constructor === Array)
      return element.toString() == [1,1].toString();
 }
    
 console.log(test.find(findArray));

Comments