admin管理员组文章数量:1026701
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
.
- 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 ifequalLength
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
2 Answers
Reset to default 7You 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;
}
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
.
- 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 ifequalLength
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
2 Answers
Reset to default 7You 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;
}
本文标签:
版权声明:本文标题:javascript - How to compare two string arrays, case insensitive and independent about ordering - JS, ES6 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745650128a2161265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论