简体   繁体   English

从数组中删除假值

[英]Deleting falsy values from an array

I'm studying at freeCodeCamp, and currently learning basic algorithms.我在 freeCodeCamp 学习,目前正在学习基本算法。 I'm doing the falsy bouncer exercise at which you need to remove all falsy values from the array.我正在做虚假的保镖练习,您需要从数组中删除所有虚假的值。 Sadly there was only the advanced answer for the task, that uses the filter() method.遗憾的是,该任务只有高级答案,即使用 filter() 方法。 I've decided to make a basic one, but currently stuck.我决定做一个基本的,但目前卡住了。

function bouncer(arr) { 

//*loops through array*    

for (let i = 0; i < arr.length; i++) {  

// *if there is a value that is falsy, delete that value from the array*    

if (arr[i] == 0 || arr[i] == NaN || arr[i] == null || arr[i] == false || arr[i] == "" || arr[i] == undefined) {        

delete arr[i];

     }    

   }    

return arr;

} 

console.log(bouncer([7, "ate", "", false, 9]));

It returns:它返回:

7,ate,,,9. 7,吃,,,9。

The function, indeed removed the falsy values, but I'm left with those three periods(,,,).该函数确实删除了虚假值,但我留下了这三个句点(,,,)。 Is there a way to make this function to work more correct and to return truthy values without those periods, without losing the simplicity of the function?有没有办法让这个函数更正确地工作,并在不失去函数的简单性的情况下返回没有这些句点的真值? Appreciate your help.感谢你的帮助。

delete only works on objects. delete仅适用于对象。 filter will do what you want: filter会做你想要的:

const arr = [7, "ate", "", false, 9]

console.log(arr.filter(x => x))

filter retains only those elements in the array for which the function returns true - here I use x => x because you only want truthy elements filter只保留数组中函数返回 true 的那些元素 - 在这里我使用x => x因为你只想要真实的元素

这会做

var m = arr.filter(Boolean);

Since you are new to Javascript and you need your code to work other than a different solution, you can do this由于您是 Javascript 的新手,并且您需要您的代码在不同的解决方案之外工作,您可以这样做

remove delete arr[i];删除delete arr[i]; and replace it with arr.splice(i,1); i--;并将其替换为arr.splice(i,1); i--; arr.splice(i,1); i--;

This will remove the item in i th position and 1 means 1 item from i th position.这将删除项目在i个位置,1表示从1项i个位置。 Since the element in i th position is removed and a new element is in there.由于第i个位置的元素被删除,并且在那里有一个新元素。 We need to continue again from there.我们需要从那里再次继续。 So we need to add i-- .所以我们需要添加i-- Otherwise in the next iteration it will start from the next element instead of i th position.否则在下一次迭代中,它将从下一个元素而不是第i个位置开始。

function bouncer(arr) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == 0 || Number.isNaN(arr[i]) || arr[i] == null || arr[i] == false || arr[i] == "" || arr[i] == undefined) {
            arr.splice(i, 1);
            i--;
        }
    }
    return arr;
}

console.log(bouncer([7, NaN, "ate", "", false, 9]));

Delete works only on Object . Delete仅适用于Object

Simply you can do it like this since you're wiling to use simple for loop简单地你可以这样做,因为你愿意使用简单的 for 循环

 function bouncer(arr) { let op =[]; for (let i = 0; i < arr.length; i++) { if(arr[i]){ op.push(arr[i]); } } return op; } console.log(bouncer([7, "ate", "", false, 9]));

Used map method and Boolean() for comparison.使用map方法和Boolean()进行比较。

const bouncer = arr => {

    let newArr = [];

    arr.map(e => {
        if (Boolean(e) == true) {
            newArr.push(e)
        }
    });


    return newArr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM