admin管理员组文章数量:1026989
I have an array with pairs of numbers and need to find matching pairs within the array
numberStore = [ [0,0],[1,1],[1,2],[1,3],[1,4],[1,5]... ]
I want to be able to find 1,4
. Is there a way to find this array without relying on numberStore[4]
?
I have an array with pairs of numbers and need to find matching pairs within the array
numberStore = [ [0,0],[1,1],[1,2],[1,3],[1,4],[1,5]... ]
I want to be able to find 1,4
. Is there a way to find this array without relying on numberStore[4]
?
- How often do you need to perform this search? – Phil Commented Dec 16, 2018 at 23:26
-
1
numberStore.find(([a, b]) => a === 1 && b === 4)
? in the same way usemap
to update needed elements – muradm Commented Dec 16, 2018 at 23:27 - Also, what exactly do you mean by "find"? Do you want the index or just to know if the pair exists? Does the order of the pair matter? – Phil Commented Dec 16, 2018 at 23:28
- 1 Need to perform search frequently and need to know if it exists – zadubz Commented Dec 16, 2018 at 23:29
3 Answers
Reset to default 4Since you need to perform this search frequently, I would build a hashed set to avoid mapping and searching over and over. For example
const numberStore = [ [0,0],[1,1],[1,2],[1,3],[1,4],[1,5] ]
const hashedSet = new Set(numberStore.map(pair => pair.toString()))
// looks like ["0,0", "1,1", "1,2", "1,3", etc]
console.log([...hashedSet])
const search = (find) => {
return hashedSet.has(find.toString())
}
console.info('Find [1,4]', search([1,4]))
console.info('Find [4,1]', search([4,1]))
I've used Array.prototype.toString()
as the hashing function but you could substitute anything there that creates a unique and parable entity for each pair.
Use Array.prototype.find()
:
var numberStore = [
[0, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5]
];
var oneFour = numberStore.find(function([a, b]) {
return a == 1 && b == 4;
});
console.log(oneFour);
Or if you prefer ES6 arrow syntax:
var numberStore = [
[0, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5]
];
var oneFour = numberStore.find(([a, b]) => a == 1 && b == 4);
console.log(oneFour);
Another alternative is using the method some() to test elements for a condition.
var numberStore = [
[0,0],
[1,1],
[1,2],
[1,3],
[1,4],
[1,5]
];
var exists = numberStore.some(([a, b]) => a === 1 && b === 4);
console.log(exists ? "Pair [1,4] exists" : "Pair [1,4] don't exists");
I have an array with pairs of numbers and need to find matching pairs within the array
numberStore = [ [0,0],[1,1],[1,2],[1,3],[1,4],[1,5]... ]
I want to be able to find 1,4
. Is there a way to find this array without relying on numberStore[4]
?
I have an array with pairs of numbers and need to find matching pairs within the array
numberStore = [ [0,0],[1,1],[1,2],[1,3],[1,4],[1,5]... ]
I want to be able to find 1,4
. Is there a way to find this array without relying on numberStore[4]
?
- How often do you need to perform this search? – Phil Commented Dec 16, 2018 at 23:26
-
1
numberStore.find(([a, b]) => a === 1 && b === 4)
? in the same way usemap
to update needed elements – muradm Commented Dec 16, 2018 at 23:27 - Also, what exactly do you mean by "find"? Do you want the index or just to know if the pair exists? Does the order of the pair matter? – Phil Commented Dec 16, 2018 at 23:28
- 1 Need to perform search frequently and need to know if it exists – zadubz Commented Dec 16, 2018 at 23:29
3 Answers
Reset to default 4Since you need to perform this search frequently, I would build a hashed set to avoid mapping and searching over and over. For example
const numberStore = [ [0,0],[1,1],[1,2],[1,3],[1,4],[1,5] ]
const hashedSet = new Set(numberStore.map(pair => pair.toString()))
// looks like ["0,0", "1,1", "1,2", "1,3", etc]
console.log([...hashedSet])
const search = (find) => {
return hashedSet.has(find.toString())
}
console.info('Find [1,4]', search([1,4]))
console.info('Find [4,1]', search([4,1]))
I've used Array.prototype.toString()
as the hashing function but you could substitute anything there that creates a unique and parable entity for each pair.
Use Array.prototype.find()
:
var numberStore = [
[0, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5]
];
var oneFour = numberStore.find(function([a, b]) {
return a == 1 && b == 4;
});
console.log(oneFour);
Or if you prefer ES6 arrow syntax:
var numberStore = [
[0, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5]
];
var oneFour = numberStore.find(([a, b]) => a == 1 && b == 4);
console.log(oneFour);
Another alternative is using the method some() to test elements for a condition.
var numberStore = [
[0,0],
[1,1],
[1,2],
[1,3],
[1,4],
[1,5]
];
var exists = numberStore.some(([a, b]) => a === 1 && b === 4);
console.log(exists ? "Pair [1,4] exists" : "Pair [1,4] don't exists");
本文标签: How do I match pairs in a 2 dimensional array in JavaScriptStack Overflow
版权声明:本文标题:How do I match pairs in a 2 dimensional array in JavaScript? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745650542a2161291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论