我的 JavaScript 第一个方法不起作用,为什么?

huangapple go评论84阅读模式
英文:

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 =&gt; {
    let pos = arr.findIndex(item =&gt; item === key1);
    while (pos !== -1) {
      arr.splice(pos, 1);
      pos = arr.findIndex(item =&gt; 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) =&gt; arr.filter(item =&gt; !exclude.includes(item))
const output = except(arr, [1, 4])
console.log(output)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月23日 13:55:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定