Loading...

Checking if at least one value in one array exists in another fails


The below should return true, but it is reporting false:

var array1 = ['fred'];
var array2 = ['sue', 'fred'];

var modified = [];

//create a new modified array that converts initials to names and vice versa 
    array1.forEach(function(element) {
            var index = array2.findIndex(el => el.startsWith(element[0]));
            if (index > -1) {
                modified.push(array2[index]);
                array2.splice(index, 1);
            } else {
                modified.push(element);
            }
    });

console.log(modified); // should be the same as array1
console.log(modified.some(v => array2.includes(v))); //false should be true

I'm trying to check if at least one value in modified exists in array2.

the opposite is also wrong:

console.log(array2.some(v => modified.includes(v))); //false should be true

JSFiddle

- - Source

Answers

answered 1 week ago mwoelk #1

array2.splice(index, 1); modifies your array2 so fred isn't contained anymore! Use slice instead of splice.

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

compared to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

answered 1 week ago Anubhav Vishwakarma #2

As the index will be determined as 1. array2.splice(index, 1); will delete the 'fred' from array2. and when you are:

console.log(modified.some(v => array2.includes(v)));

array2 is actually ['sue'] and modified is ['fred'].

Hence evaluating to false.

You should use Array.prototype.slice() instead of Array.prototype.splice() if you dont want to modify the array2

answered 1 week ago p.s.w.g #3

The problem is in this line:

array2.splice(index, 1);

You're actually removing the found item from array2 so of course if you look for that item to be in array2 later it won't be found. Observe:

var array1 = ['fred'];
var array2 = ['sue', 'fred'];
var modified = [];

//create a new modified array that converts initials to names and vice versa 
array1.forEach(function(element) {
  var index = array2.findIndex(el => el.startsWith(element[0]));
  if (index > -1) {
    modified.push(array2[index]);
    array2.splice(index, 1);
  } else {
    modified.push(element);
  }
});

console.log("modified: ", ...modified); // should be the same as array1
console.log("array2: ", ...array2);     // array2 has been modified

One quick solution is to clone array array2 before you start modifying it, and then do your work on the clone instead:

var array1 = ['fred'];
var array2 = ['sue', 'fred'];
var modified = [];
var filtered = [...array2];

//create a new modified array that converts initials to names and vice versa 
array1.forEach(function(element) {
  var index = filtered.findIndex(el => el.startsWith(element[0]));
  if (index > -1) {
    modified.push(array2[index]);
    filtered.splice(index, 1);
  } else {
    modified.push(element);
  }
});

console.log(modified.some(v => array2.includes(v))); // true

comments powered by Disqus