英文:
Is there a better way to make this function return true as expected
问题
我有一个JS逻辑,我正在尝试实现,现在正在思考为什么它不起作用:我有两个数组,它们至少有一个相似的元素,但实现的逻辑返回了false
- 但我期望的结果是相反的。请看一下:
const array_one = [
"Apparel",
"Footwear",
];
const array_two = [
"Soap",
"Footwear",
];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
}
return true;
};
console.log(checkArray(array_one, array_two)); //将`false`记录到控制台,期望为'true'。
希望这对你有帮助。
英文:
I have a JS logic that I am trying to implement, and now pondering why it is not working: I have two arrays with at least one similar element in them, but implementing the logic returned false
- but I was expecting the opposite result. Please take a look:
const array_one = [
"Apparel",
"Footwear",
];
const array_two = [
"Soap",
"Footwear",
];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
}
return true;
};
console.log(checkArray(array_one, array_two)); //logs false to the console, was expecting 'true'.
答案1
得分: 3
- 使用
some
方法来检查一个数组中是否存在另一个数组的任何值:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
return arr1.some(value => arr2.includes(value));
}
};
console.log(checkArray(array_one, array_two)); //true
- 使用
filter
方法:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
return arr1.filter(value => arr2.includes(value)).length > 0;
}
};
console.log(checkArray(array_one, array_two)); //true
- 使用
Set
和some
方法,这更高效,因为Set
的has
方法的平均时间复杂度为 O(n),而且不需要遍历整个数组:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
const set = new Set(arr2);
return arr1.some(value => set.has(value));
}
};
console.log(checkArray(array_one, array_two)); //true
- 使用
reduce
方法:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
return arr1.reduce((accumulator, value) => accumulator || arr2.includes(value), false);
}
};
console.log(checkArray(array_one, array_two)); //true
注意:方法1. filter
和 2. reduce
不太高效,因为它们创建新数组并累积值,这可能对较大的数组来说会占用内存并变慢。
英文:
- Using
some
method to check if any value from one array exists in another array:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
return arr1.some(value => arr2.includes(value));
}
};
console.log(checkArray(array_one, array_two)); //true
<!-- end snippet -->
- Using
filter
method:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
return arr1.filter(value => arr2.includes(value)).length > 0;
}
};
console.log(checkArray(array_one, array_two)); //true
<!-- end snippet -->
- Using the
some
method with theSet
which is more performant since it has an average time complexity of O(n) for thehas
method, and does not require iterating through the entire array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
const set = new Set(arr2);
return arr1.some(value => set.has(value));
}
};
console.log(checkArray(array_one, array_two)); //true
<!-- end snippet -->
- Using the
reduce
method:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array_one = [ "Apparel", "Footwear", ]; const array_two = [ "Soap", "Footwear", ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false;
} else {
return arr1.reduce((accumulator, value) => accumulator || arr2.includes(value), false);
}
};
console.log(checkArray(array_one, array_two)); //true
<!-- end snippet -->
Note: methods 1. filter
and 2. reduce
are less performant , since they create new arrays, accumulate values, which can be memory-intensive and slow for larger arrays.
答案2
得分: 0
只需将逻辑颠倒过来。
- 将
false
设为默认假设 - 为每个输入
Array
创建一个Set
,以便进行O(1)
查找 - 检查集合中当前项目是否存在于另一个集合中,如果是,则立即返回
true
英文:
Just flip your logic around.
- Make
false
the default assumption - Create a
Set
for each inputArray
forO(1)
lookup - Check if the current item in the set exists in the other and immediately return
true
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
array_one = [ "Apparel" , "Footwear" ],
array_two = [ "Soap" , "Footwear" ],
array_three = [ "Apple" , "Orange" ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false; // lengths must match
}
const
set1 = new Set(arr1),
set2 = new Set(arr2);
for (const [value] of set1.entries()) {
if (set2.has(value)) {
return true; // short-circuit with true
}
}
return false; // default assumption
};
console.log(checkArray(array_one, array_two)); // true
console.log(checkArray(array_one, array_three)); // false
<!-- end snippet -->
If you think Set
is overkill, you can always use Array.prototype.includes
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
array_one = [ "Apparel" , "Footwear" ],
array_two = [ "Soap" , "Footwear" ],
array_three = [ "Apple" , "Orange" ];
const checkArray = (arr1, arr2) => {
if (arr1.length !== arr2.length) {
return false; // lengths must match
}
for (let i = 0; i < arr1.length; i++) {
if (arr2.includes(arr1[i])) {
return true; // short-circuit with true
}
};
return false; // default assumption
};
console.log(checkArray(array_one, array_two)); // true
console.log(checkArray(array_one, array_three)); // false
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论