删除与参数匹配的数组元素并返回新数组

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

Removing array elements that match with arguments and returning a new array

问题

我在传递的数组上使用forEach()。我传递给forEach()的函数是,如果arrays的元素与给定的某个参数不匹配,则将其推送到newArray中。

然而,我一直得到一个空数组,而我想看到的是newArray = [1, 2]

我在想我的if语句可能有问题,但我真的看不出问题在哪。我似乎无法找到问题所在。

const removeFromArray = function(arrays, ...args) {
    const newArray = [];
    
    arrays.forEach((element) => {
        if (!args.includes(element)) {
            newArray.push(element);
        }
    });

    return newArray;
}

removeFromArray([1,2,3], 3);
英文:

I am using the forEach() on the array that is passed through. The function I am passing into forEach() is if the element of arrays does not match with one of the arguments given, then push it into the newArray.

However, I keep getting a blank array, when I want to see newArray = [1, 2].

I am thinking there is something wrong with my if statement, but I really don't see what the problem is. I can't seem to put a finger on the issue.

const removeFromArray = function(arrays, ...args) {
    const newArray = [];
    
    for (const arg of args) {
        if (arrays.forEach((element) => element !== arg))
        newArray.push(element);        
    }
    return newArray;
}

removeFromArray([1,2,3], 3);

答案1

得分: 0

尝试使用Array.filter()方法

const removeFromArray = function(arrays, args) {
    return arrays.filter(ele => ele !== args)
}

removeFromArray([1,2,3], 3);
英文:

Try using Array.filter() method

const removeFromArray = function(arrays, args) {
    return arrays.filter(ele => ele !== args)
}

removeFromArray([1,2,3], 3);

答案2

得分: 0

以下是翻译好的部分:

  1. I will assume that you are doing this for a learning experience.
    我会假设你这样做是为了学习经验。

  2. As others have said, filter is what you should be using.
    正如其他人所说,你应该使用 filter。

  3. With that said you had a couple errors in your code.
    话虽如此,你的代码中有一些错误。

  4. You did if(array.forEach... which can not be evaluated as its a iterator and is not truthy
    你写了 if(array.forEach...),这不能被评估,因为它是一个迭代器并且不是真值。

  5. You needed to push the element inside the forEach, not outside if it
    你需要在 forEach 内部推送元素,而不是在外部 if 语句。

  6. const removeFromArray = function(arrays, ...args) {
    const removeFromArray = function(arrays, ...args) {

  7. const newArray = [];
    const newArray = [];

  8. for (const arg of args) {
    for (const arg of args) {

  9. arrays.forEach(element => {
    arrays.forEach(element => {

  10. if (element !== arg){
    if (element !== arg){

  11. newArray.push(element);
    newArray.push(element);

  12. }
    }

  13. return newArray;
    return newArray;

  14. console.log(removeFromArray([1,2,3], 3));
    console.log(removeFromArray([1,2,3], 3));

英文:

I will assume that you are doing this for a learning experience. As others have said, filter is what you should be using. With that said you had a couple errors in your code.

  1. You did if(array.forEach... which can not be evaluated as its a iterator and is not truthy
  2. You needed to push the element inside the forEach, not outside if it

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const removeFromArray = function(arrays, ...args) {
    const newArray = [];
    
    for (const arg of args) {
        arrays.forEach(element =&gt; {
           if(element !== arg){
               newArray.push(element);               
           }
        })
    }
    return newArray;
}

console.log(removeFromArray([1,2,3], 3));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 22:29:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685937.html
匿名

发表评论

匿名网友

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

确定