英文:
How to check sub array exist in an array using javascript
问题
我正在尝试检查一个子数组是否存在于一个数组中,以便我可以丢弃或避免添加。
例如
假设我有这样一个数组
[
[-5, 0, 5], [-5, 2, 3],
[-3, 0, 3,], [-3, 1, 2],
[-1, -1, 2], [-1, 0, 1]
]
并且有另一对作为
[-3, 1, 2]
要添加,但它已经存在于现有数据中,所以需要检查并避免添加。
我迄今为止尝试过的是
let mapped = result.map(item => item.join());
let match = `${item1},${item2},${item3}`; // 即将添加的项目 => item1, item2, item3
if(mapped.includes(match)){
...
} else {
...
}
这样使用是否可以,或者有没有更好的方法来在有大量数据时进行检查?
英文:
I'm trying to check whether a sub-array exists in an array so that I could discard or avoid adding.
For example
Let's assume I have an array like this
[
[-5, 0, 5], [-5, 2, 3],
[-3, 0, 3,], [-3, 1, 2],
[-1, -1, 2], [-1, 0, 1]
]
and got another pair as
[-3, 1, 2]
to add but it's already available in existing data so need to check and avoid adding.
What I have tried so far
let mapped = result.map(item => item.join());
let match = `${item1},${item2},${item3}`; // coming items => item1, item2, item3
if(mapped.includes(match)){
...
} else {
...
}
Is this okay to use or any better way to check it when we have a huge amount of data?
答案1
得分: 1
Here is the translated code snippet with the array methods .find()
and .every()
:
const
arr = [
[-5, 0, 5], [-5, 2, 3],
[-3, 0, 3,], [-3, 1, 2],
[-1, -1, 2], [-1, 0, 1]
],
input = [-3, 1, 2],
isIn = (a, b) => b.find(x => x.every((y, i) => y === a[i]))?.length > 0;
console.log( isIn(input, arr), arr.length );
isIn(input, arr) || arr.push(input);
console.log( arr.length );
isIn([-3, 1, -2], arr) || arr.push([-3, 1, -2]);
console.log( arr.length );
Please note that I've translated only the code portion as requested, and I've removed the unnecessary comments and HTML escape characters.
英文:
Using array methods .find()
and .every()
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
arr = [
[-5, 0, 5], [-5, 2, 3],
[-3, 0, 3,], [-3, 1, 2],
[-1, -1, 2], [-1, 0, 1]
],
input = [-3, 1, 2],
isIn = (a,b) => b.find(x => x.every((y,i) => y === a[i]))?.length > 0;
console.log( isIn(input, arr), arr.length );
isIn(input, arr) || arr.push(input);
console.log( arr.length );
isIn([-3, 1, -2], arr) || arr.push([-3, 1, -2]);
console.log( arr.length );
<!-- end snippet -->
答案2
得分: 1
我只会返回代码的翻译部分,如下:
我只会迭代您的数组并检查元素是否等于要添加的元素。假设您只处理数字,您可以通过将数组转换为字符串来检查。
const data = [
[-5, 0, 5],
[-5, 2, 3],
[-3, 0, 3],
[-3, 1, 2],
[-1, -1, 2],
[-1, 0, 1]
];
const toAdd = [-3, 1, 2];
const isRowIncludedInMatrix = (matrix, row) =>
matrix.some(r => r.toString() === row.toString());
if (!isRowIncludedInMatrix(data, toAdd)) // => false -> don't push
data.push(toAdd);
请注意,这是代码的翻译部分,不包括您的原始问题。
英文:
I would just iterate over your array and check if an element is equal to the element you want to add. Assuming you are only dealing with numbers, you can check that by converting your array to a string.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data = [
[-5, 0, 5],
[-5, 2, 3],
[-3, 0, 3],
[-3, 1, 2],
[-1, -1, 2],
[-1, 0, 1]
];
const toAdd = [-3, 1, 2];
const isRowIncludedInMatrix = (matrix, row) =>
matrix.some(r => r.toString() === row.toString());
if (!isRowIncludedInMatrix(data, toAdd)) // => false -> don't push
data.push(toAdd);
<!-- end snippet -->
答案3
得分: 1
如果这是你要处理的唯一数据,简单的比较可能是最好的选择。
some() 和 every() 函数会提前退出:
function addIfNotExists(arrays, newArray) {
if (!arrays.some(x => x.length === newArray.length && x.every((e, i) => e === newArray[i])))
arrays.push(x);
}
英文:
If that is the only data you have to work with, a simple comparison might be best.
The some() and every() functions will early exit:.
function addIfNotExists(arrays, newArray) {
if (!arrays.some(x => x.length === newArray.length && x.every((e, i) => e === newArray[i])))
arrays.push(x);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论