Sweepster
Sweepster

Reputation: 1949

Checking is array contains some elements of another returns incorrect result

The below code checks whether array1 or array2 contain hyphens under certain conditions. If they do, those hyphens are removed. I then compare the two arrays to check whether some elements in one array are contained in the other:

var array1 = ['banana-orange'];
var array2 = ['banana', 'orange'];

if (checkHyphen(array2) == true && checkHyphen(array1) == false) {
    for (let i = 0; i < array2.length; i++) {
        if (array2[i].includes('-')) {
            array2[i] = array2[i].replace('-', ' ').split(' ');
        }
    }
} else if (checkHyphen(array2) == false && checkHyphen(array1) == true) {
    for (let i = 0; i < array1.length; i++) {
        if (array1[i].includes('-')) {
            array1[i] = array1[i].replace('-', ' ').split(' ');
        }
    }
}

function checkHyphen(a) {
    var finished = false;

    for (let i = 0; i < a.length; i++) {
        if (a[i].includes('-')) {
            finished = true;
        }
    }

    return finished;
}

console.log('array2 = ' + array2); //should match array1 - it does
console.log('array1 = ' + array1); //should match array2 - it does
console.log(array2.some(v => array1.includes(v)) == true); // returns false, should be true

If both arrays match, then clearly some() should return true, but it doesn't. How can I fix this?

Note: array1 and array2 DO NOT always have to match exactly, one or the other might have more values. I just need to test if at least one element exists in the other.

JSfiddle

Upvotes: 2

Views: 99

Answers (2)

Maheer Ali
Maheer Ali

Reputation: 36584

The problem is that you are setting elements of array to another array. So it create nested array. You should flat() them before checking.

var array1 = ['banana-orange'];
var array2 = ['banana', 'orange'];

if (checkHyphen(array2) == true && checkHyphen(array1) == false) {
            for (let i = 0; i < array2.length; i++) {
                if (array2[i].includes('-')) {
                    array2[i] = array2[i].replace('-',' ').split(' ');
                }               
            }                       
        } else if (checkHyphen(array2) == false && checkHyphen(array1) == true) { 
            for (let i = 0; i < array1.length; i++) {
                if (array1[i].includes('-')) {
                    array1[i] = array1[i].replace('-',' ').split(' ');
                }
            }
        }       

function checkHyphen(a) {   
        var finished = false;

        for (let i = 0; i < a.length; i++) {
            if (a[i].includes('-')) {
                finished = true;
            }               
        }

        return finished;
    }


array2 = array2.flat();
array1 = array1.flat();
console.log(array2.some(v => array1.includes(v)) == true);

You can use JSON.stringify() as alternate for flat()

array2 = String(array2).split(',');
array1 = String(array1).split(',');

Here is a short version of your code.

var array1 = ['banana-orange'];
var array2 = ['banana', 'orange'];

if (checkHyphen(array1) && !checkHyphen(array2)) {
     array1 = array1.flatMap(x => x.split('-'))                
} else if (checkHyphen(array2) && !checkHyphen(array1)) { 
     array2 = array2.flatMap(x => x.split('-'))  
}       

function checkHyphen(a) {   
        return String(a).includes('-');
    }

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

Upvotes: 1

user4639281
user4639281

Reputation:

Instead of trying to check for hyphens and such, you should just define a function to process the individual input arrays, splitting each value on any hyphens and pushing the result onto a new array.

const input1 = ['banana-orange', 'mango']
const input2 = ['banana', 'orange', 'apple-grape']
const input3 = ['mango']

function check(input1, input2) {
  const process = input => input.reduce((result, value) => (result.push(...value.split(/-+/g)), result), [])
  const processed = [input1, input2].map(input => process(input))
  return processed[0].some(value => processed[1].includes(value))
}

console.log(check(input1, input2))
console.log(check(input1, input3))
console.log(check(input2, input3))

Upvotes: 0

Related Questions