1

I am trying to see if a particular pair of numbers exist in an array. I know that .some() works effectively when it's a simple array, but I can't seem to get it to work in nested arrays.

Thank you in advance.

const array = [[1, 0], [2, 0], [3, 1], [4, 3], [5, 2]];

// checks whether an element exists
const exists = (i) => i == [3, 1] || [1, 3];

console.log(array.some(exists));
// expected output: true
peyo
  • 351
  • 4
  • 15
  • Does this answer your question? [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – VLAZ Oct 20 '20 at 19:31
  • 3
    It's returning true for the wrong reason. – Barmar Oct 20 '20 at 19:32
  • Also relevant( but the dupe is definitely more important): [Check variable equality against a list of values](https://stackoverflow.com/q/4728144) – VLAZ Oct 20 '20 at 19:33
  • @VLAZ - I think I can derive an answer from "How to compare arrays in JavaScript" but I don't think it's the same problem. In that particular post, one is trying to see if two arrays are the same. In my particular problem, I'm trying to see if an array exists within another array. – peyo Oct 20 '20 at 19:37
  • 1
    @peyo yes, and your effort *does* involve comparing arrays. The important thing is that `==`/`===` will *not* say that two arrays are the same if they have the same items, it literally has to be *the same* array. Therefore, if you want to find if a particular array is contained within another, you still need to compare the arrays by their contents. – VLAZ Oct 20 '20 at 19:39

3 Answers3

1

You need to check every value because of different arrays, you have different object references, and you misused the logical OR operator. This does not include a comparison with another value.

const array = [[1, 0], [2, 0], [3, 1], [4, 3], [5, 2]];

const exists = ([a, b]) => a === 3 && b === 1 || a === 1 && b === 3;

console.log(array.some(exists));
peyo
  • 351
  • 4
  • 15
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Here is a general solution.

const array = [[1, 0], [2, 0], [3, 1], [4, 3], [5, 2]];
const expected = [[1, 3], [3, 1]];
const exists = (i) => expected.some((j) => {
    return i.every((k, n) => j[n] === k);
});

console.log(array.some(exists));

This could be wrapped in a function to make it reusable.

const array = [[1, 0], [2, 0], [3, 1], [4, 3], [5, 2]];
const expected = [[1, 3], [3, 1]];

const containsAny = (array, expected) => {
  return array.some((i) => expected.some((j) => {
    return i.every((k, n) => j[n] === k);
  }))
};


console.log(containsAny(array, expected));
Brenden
  • 1,947
  • 1
  • 12
  • 10
0

You could make a custom function out of it which makes it reusable. This is a simple example. By flattening the array you can check if values are the same.

Got my inspiration on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

const array = [[1, 0], [2, 0], [3, 1], [4, 3], [5, 2]];

const checkArrayFor = (needle, haystack) => {
  const flattenNeedle = needle.join(':');
  const flattenArray = haystack.map(item => item.join(':'));
  return flattenArray.includes(flattenNeedle);
};

console.log("Check for [1, 0]", checkArrayFor([1, 0], array));
console.log("Check for [1, 1]", checkArrayFor([1, 1], array));
Stinodotbe
  • 402
  • 3
  • 10