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)

  • 1
    I do not want to loop through it though- No other way. You cannot directly compare arrays – ellipsis Oct 02 '19 at 18:01
  • You could do `return JSON.stringify(element) == JSON.stringify([1, 1]);`. I don't really like that solution myself though. –  Oct 02 '19 at 18:13
  • Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Heretic Monkey Oct 02 '19 at 18:23

2 Answers2

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));
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 1
    "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" – Wendelin Oct 02 '19 at 18:08
  • @CodeManiac could I pass the searched array as an argument? –  Oct 02 '19 at 18:21
  • @CodeManiac whe I try to add an argument to findArray I get: "Uncaught TypeError: Cannot read property 'length' of undefined –  Oct 02 '19 at 18:26
  • @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` – Code Maniac Oct 02 '19 at 18:31
  • _“You need to match each element with respective […]”_ — that sentence is not complete. What do you mean here? – Sebastian Simon Oct 11 '20 at 10:51
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));
Charlie
  • 22,886
  • 11
  • 59
  • 90