How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
10 plus years* active enterprise development experience and a Fine art degree 🎨
// sparse array - i.e. has holesconsta=[,,2,,,];// dense array - no holesconstb=[undefined,undefined,2,undefined,undefined];console.log(`a: ${a.length}`);// "a: 5" i.e. last comma is treated as a trailing commaconsole.log(`b: ${b.length}`);// "b: 5"functionpredicate(value,index){console.log(index);returnBoolean(value);}constaf=a.filter(predicate);// 2constbf=b.filter(predicate);// 0, 1, 2, 3, 4console.log(af);// [2]console.log(bf);// [2]
i.e. holes are skipped, actual undefined values are not.
So the function passed to filter() never gets to make a decision about the holes.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
10 plus years* active enterprise development experience and a Fine art degree 🎨
The intent is to remove All falsey values which I admit is a bit of a hammer and yes I can see your point, filter ignores empties, but it still cleans an array which is my point.
To anyone not aware, This is a well known trick which removes ALL falsey value.
If you want to handle falsey valuess such as 0, -n, false, null, undefined, write a better predicate function, my demo is just that, a quick demo of principal, prepare your inputs.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It doesn't matter what function you pass to
filter()
- it skips holes.ECMAScript 6: holes in Arrays.
I imagine you misunderstood my code
I imagine that you were trying to use
to sanitize the array. However observe
i.e. holes are skipped, actual
undefined
values are not.So the function passed to
filter()
never gets to make a decision about the holes.sparse arrays vs. dense arrays
The intent is to remove All falsey values which I admit is a bit of a hammer and yes I can see your point, filter ignores empties, but it still cleans an array which is my point.
To anyone not aware, This is a well known trick which removes ALL falsey value.
If you want to handle falsey valuess such as 0, -n, false, null, undefined, write a better predicate function, my demo is just that, a quick demo of principal, prepare your inputs.