admin管理员组文章数量:1022603
I have an array like so:
const arr = [[1, 2], [3, 4], [5, 7]]
Say I have two numbers: 3
and 4
, how do I check if a pair of those two numbers (regardless of it's order exists in the array?
I have an array like so:
const arr = [[1, 2], [3, 4], [5, 7]]
Say I have two numbers: 3
and 4
, how do I check if a pair of those two numbers (regardless of it's order exists in the array?
2 Answers
Reset to default 6You can use Array#findIndex
to get the index
of the pair or -1
if not found:
const arr = [[1, 2], [3, 4], [5, 7]];
const searchForPairInList = (a, b) =>
arr.findIndex(([first, second]) =>
(first === a && second === b) || (first === b && second === a)
);
console.log( '3, 4:', searchForPairInList(3, 4) );
console.log( '7, 5:', searchForPairInList(7, 5) );
console.log( '3, 3:', searchForPairInList(3, 3) );
EDIT: The following method only works when the two numbers are unique, i.e when a !== b
This can be done with a for
loop and the built in Array.includes()
, which returns a Boolean if the specified value is present in the array
const arr = [[1, 2], [3, 4], [5, 7], [3, 4]];
function hasPair(array, a, b) {
let pairStatus = false;
for(let pair of arr) {
if (pair.includes(a) && pair.includes(b)) {
pairStatus = true;
}
}
return pairStatus;
}
console.log(hasPair(arr, 1, 4)); // false
console.log(hasPair(arr, 3, 4)); // true
If you want to find how many instances of the pair exist in the array, you can just replace the pairStatus
variable with a counter to increment
const arr = [[1, 2], [3, 4], [5, 7], [3, 4]];
function instancesOfPair(array, a, b) {
let instances = 0;
for(let pair of arr) {
if (pair.includes(a) && pair.includes(b)) {
instances++;
}
}
return instances;
}
console.log(instancesOfPair(arr, 3, 4)); // 2
I have an array like so:
const arr = [[1, 2], [3, 4], [5, 7]]
Say I have two numbers: 3
and 4
, how do I check if a pair of those two numbers (regardless of it's order exists in the array?
I have an array like so:
const arr = [[1, 2], [3, 4], [5, 7]]
Say I have two numbers: 3
and 4
, how do I check if a pair of those two numbers (regardless of it's order exists in the array?
2 Answers
Reset to default 6You can use Array#findIndex
to get the index
of the pair or -1
if not found:
const arr = [[1, 2], [3, 4], [5, 7]];
const searchForPairInList = (a, b) =>
arr.findIndex(([first, second]) =>
(first === a && second === b) || (first === b && second === a)
);
console.log( '3, 4:', searchForPairInList(3, 4) );
console.log( '7, 5:', searchForPairInList(7, 5) );
console.log( '3, 3:', searchForPairInList(3, 3) );
EDIT: The following method only works when the two numbers are unique, i.e when a !== b
This can be done with a for
loop and the built in Array.includes()
, which returns a Boolean if the specified value is present in the array
const arr = [[1, 2], [3, 4], [5, 7], [3, 4]];
function hasPair(array, a, b) {
let pairStatus = false;
for(let pair of arr) {
if (pair.includes(a) && pair.includes(b)) {
pairStatus = true;
}
}
return pairStatus;
}
console.log(hasPair(arr, 1, 4)); // false
console.log(hasPair(arr, 3, 4)); // true
If you want to find how many instances of the pair exist in the array, you can just replace the pairStatus
variable with a counter to increment
const arr = [[1, 2], [3, 4], [5, 7], [3, 4]];
function instancesOfPair(array, a, b) {
let instances = 0;
for(let pair of arr) {
if (pair.includes(a) && pair.includes(b)) {
instances++;
}
}
return instances;
}
console.log(instancesOfPair(arr, 3, 4)); // 2
本文标签: javascriptJs check if pair exists in array of pairsStack Overflow
版权声明:本文标题:javascript - Js check if pair exists in array of pairs - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745509758a2153773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论