admin管理员组

文章数量:1435208

I want to pare two string arrays, but case insensitive and independent.

For the example:

['a', 'b', 'c'] === ['A', 'c', 'B'] -> TRUE

['a', 'b', 'c'] === ['a', 'b', 'd'] -> FALSE

TRUE when they are with the same length and same values (case insensitive ['A'] === ['a'] -> true) and independent, about ordering ['a', 'b'] === ['b', 'a'] -> true.

What I did for now is:

areEqual = (arr1, arr2) => {
    const equalLength = arr1.length === arr2.length;

    return arr2.every(arr2Item => {

        return arr1.includes(arr2Item.toLowerCase());

    }) && equalLength;
};

, but this is case sensitive.

I am using JS, ES6 with React.

I want to pare two string arrays, but case insensitive and independent.

For the example:

['a', 'b', 'c'] === ['A', 'c', 'B'] -> TRUE

['a', 'b', 'c'] === ['a', 'b', 'd'] -> FALSE

TRUE when they are with the same length and same values (case insensitive ['A'] === ['a'] -> true) and independent, about ordering ['a', 'b'] === ['b', 'a'] -> true.

What I did for now is:

areEqual = (arr1, arr2) => {
    const equalLength = arr1.length === arr2.length;

    return arr2.every(arr2Item => {

        return arr1.includes(arr2Item.toLowerCase());

    }) && equalLength;
};

, but this is case sensitive.

I am using JS, ES6 with React.

Share Improve this question edited Dec 20, 2018 at 19:45 gdfgdfg asked Dec 20, 2018 at 18:49 gdfgdfggdfgdfg 3,5868 gold badges46 silver badges85 bronze badges 7
  • Possible duplicate of How to pare arrays in JavaScript? – Keno Commented Dec 20, 2018 at 18:51
  • Is this correct? ['a', 'b', 'c'] === ['a', 'c'] -> TRUE – Alexander O'Mara Commented Dec 20, 2018 at 18:53
  • 3 How can ['a', 'b', 'c'] === ['a', 'c'] -> TRUE be true if equalLength is false? – Andy Commented Dec 20, 2018 at 18:54
  • 1 What is your expectation about ['a', 'a', 'b'] and ['b', 'b', 'a']? Should they be equal? They have the same length and the same set of values, but each with different multiplicities. If you say they're equal, then shouldn't ['a', 'a', 'a', 'b'] also be equal to them? – Scott Sauyet Commented Dec 20, 2018 at 18:59
  • 3 Your third example still says TRUE for arrays of different lengths. – T.J. Crowder Commented Dec 20, 2018 at 18:59
 |  Show 2 more ments

2 Answers 2

Reset to default 7

You could normalise the strings to lower case and use a Set for checking the values.

function pare(a, b) {
    const lower = s => s.toLowerCase();
    return b
        .map(lower)
        .every(Set.prototype.has, new Set(a.map(lower)));
}

console.log(pare(['a', 'b', 'c'], ['A', 'c', 'B'])); //  true
console.log(pare(['a', 'b', 'c'], ['a', 'b', 'd'])); // false
console.log(pare(['a', 'b', 'c'], ['a', 'c']));      //  true

You need to replace arr1 with a lowercase copy of it. You should also return immediately if they're not the same length, rather than going through all the parison work when it's not necessary.

areEqualCI = (arr1, arr2) => {
    if (arr1.length != arr2.length) {
        return false;
    }
    const arr1Lower = arr1.map(e => e.toLowerCase());
    return arr2.every(arr2Item => {
        return arr1Lower.includes(arr2Item.toLowerCase());
    });
};

It might also be better to sort the two arrays, then just pare them elementwise:

areEqualCI = (arr1, arr2) => {
    if (arr1.length != arr2.length) {
        return false;
    }
    const arr1Lower = arr1.map(e => e.toLowerCase()).sort();
    const arr2Lower = arr2.map(e => e.toLowerCase()).sort();
    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i] != arr2[i]) {
            return false;
        }
    }
    return true;
}

本文标签: