'Pattern Matching in javascript returning true if first array has 2 and second array has 22 How do i solve this?

I have two arrays one of it is having user id and another one is having user ids. Those arrays are as follows.

1)The array which is having user id. data[key].effective_employees Which is eaqual to [2].

Now I have another array which is having numbers of employee ids which is as follows. data2[0].id Which is eaqual to [2,22,21].

And now I am trying to see whether the array two has number in array 1 I am using the following logic to see whether it is working or not.

if ((/^\d+$/.test(_.intersection([data2[0].id.toString()], data[key].effective_employees)))) {
    let isElem = _.contains(returnStackFilterd, value);
    if (isElem == false) {
        returnStackFilterd.push(value);
    }
} else {
    returnStackFilterd = _.without(returnStackFilterd, value);
}

But this is showing true for the number 2 if the array two is having 22. Psudo code of what is happening with it is as follows.

if([2]is in[22,21]){ it is printing true} I want false here as the number two is not in the second array. The second array contains 22 and 21 which is not eaqual to 2

How do i solve this problem? The above psudo code should print false.



Solution 1:[1]

Let's break down your test expression and see why it doesn't work.

First off, we know that data[key].effective_employees is [2]. data2[0].id might be [2, 22, 21] or [22, 21]. If I'm understanding your question correctly, you want the whole test expression to return true in the first case and false in the second case.

Rebuilding your test expression from the bottom up, the innermost expression we find is this:

data2[0].id.toString()

This is a string with the value '2,22,21' or '22,21', depending on which case we are talking about. Next, you wrap this string in an array:

[data2[0].id.toString()]

So now we have ['2,22,21'] or ['22,21']. Note the quotes; in either case, it is an array with a single element that is a string.

Next, you take the intersection of this array with data[key].effective_employees, which we know is [2]:

_.intersection([data2[0].id.toString()], data[key].effective_employees)

So this expression is effectively

_.intersection(['2,22,21'], [2])

or

_.intersection(['22,21'], [2])

You are always taking the intersection of two arrays, where the first contains a single string and the second contains a number. They can't have any elements in common, so that's always going to produce an empty array ([]).

Finally, you test whether that empty array matches a regular expression:

/^\d+$/.test(_.intersection([data2[0].id.toString()], data[key].effective_employees))
// is effectively the same as
/^\d+$/.test([])

Regular expressions are supposed to be matched against a string, not an array. JavaScript is very lenient in situations like these and will coerce the value you're passing to a string. That means that the value [] is first converted to the empty string '' before being matched to the regular expression /^\d+$/. The empty string does not contain any digits, so this test always returns false.

This is why your test doesn't work as intended. However, let's take a few steps back, because you seem to be doing many things you don't need to do. Why convert arrays to strings (and then back to array)? Why match against a regular expression, if you just want to know whether two arrays have elements in common?

The following, simpler expression will give you the elements that data[key].effective_employees and data2[0].id have in common:

_.intersection(data[key].effective_employees, data2[0].id)

This will evaluate to either [2] or [], depending on whether data[key].effective_employees contains the number 2 or not.

I suggest saving the result of this expression to a variable, because it makes your code easier to read. Let's call it commonIds:

const commonIds = _.intersection(data[key].effective_employees, data2[0].id)

Now you can formulate different conditions, based on what exactly you want this intersection to be like. My impression is that you just want it to be nonempty (i.e., at least one element in common). In that case, you can compare its length to zero:

if (commonIds.length > 0) {
    // code for when there is an overlap
} else {
    // code for when there is no overlap
}

As a final note, I recommend assigning your base expressions data[key].effective_employees and data2[0].id to variables as well. Again, this makes your code more readable, and it also ensures that you need to change only one line of code if those base expressions change. Putting it all together:

const key = 'x';
const data = { [key]: {
    effective_employees: [2],
}};
const data2 = [{
    id: [2, 22, 21],
}];

const userId = data[key].effective_employees;
const employeeIds = data2[0].id;
const commonIds = _.intersection(userId, employeeIds);

if (commonIds.length > 0) {
    console.log('userId appears in employeeIds');
} else {
    console.log('userId does not appear in employeeIds');
}
<script src="https://underscorejs.org/underscore-umd-min.js"></script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman