英文:
my javascript first method is not working , why?
问题
以下是代码部分的翻译:
第一部分代码中的问题是什么?
// 这部分代码有问题
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
function except(arr, exclude) {
for (let key in arr) {
for (let key1 in exclude) {
if (arr[key] === exclude[key1]) {
arr.splice(key, 1)
}
}
}
console.log(arr)
}
except(arr, [2, 4])
// 期望结果是 [1, 1, 5, 3]
// 实际结果是 [1, 1, 5, 2, 3]
如果在排除列表 exclude
中的第二个值在数组 arr
中只出现一次,那么这段代码可以正常工作。
我尝试了下面的方法,它有效...
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
function except(arr, exclude) {
let final = [];
for (let key of arr) {
if (!exclude.includes(key))
final.push(key)
}
return final;
}
const output = except(arr, [1, 4])
console.log(output)
英文:
Can anyone explain what is wrong in 1st section of the code?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// this is not working
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
function except(arr, exclude) {
for (let key in arr) {
for (let key1 in exclude) {
if (arr[key] === exclude[key1]) {
arr.splice(key, 1)
}
}
}
console.log(arr)
}
except(arr, [2, 4])
// expecting [1, 1, 5, 3]
// getting [1, 1, 5, 2, 3]
<!-- end snippet -->
If the 2nd value in exclude is present only one time in the arr, then it's working fine
I tried this method and it is working....
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
function except(arr, exclude) {
let final = [];
for (let key of arr) {
if (!exclude.includes(key))
final.push(key)
}
return final;
}
const output = except(arr, [1, 4])
console.log(output)
<!-- end snippet -->
答案1
得分: 1
Splice works on position - 你尝试在位置上使用 splice。
Here is a possible fix
以下是一个可能的修复:
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
function except(arr, exclude) {
exclude.forEach(key1 => {
let pos = arr.findIndex(item => item === key1);
while (pos !== -1) {
arr.splice(pos, 1);
pos = arr.findIndex(item => item === key1);
}
});
console.log(arr);
}
except(arr, [2, 4]);
Why not filter?
为什么不使用 filter?
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
const except = (arr, exclude) => arr.filter(item => !exclude.includes(item))
const output = except(arr, [1, 4])
console.log(output)
英文:
Splice works on position - you try to splice on the key as position.
Here is a possible fix
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
function except(arr, exclude) {
exclude.forEach(key1 => {
let pos = arr.findIndex(item => item === key1);
while (pos !== -1) {
arr.splice(pos, 1);
pos = arr.findIndex(item => item === key1);
}
});
console.log(arr);
}
except(arr, [2, 4]);
<!-- end snippet -->
Why not filter?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let arr = [1, 2, 1, 4, 5, 4, 2, 3];
const except = (arr, exclude) => arr.filter(item => !exclude.includes(item))
const output = except(arr, [1, 4])
console.log(output)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论