loading...

re: 5 ways to delete an element from an array in JavaScript VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Not sure how spread makes it more clear?!?! Array.prototype.slice is literally intended and is know to make a copy of an array.... From MDN: The...

I said I don't agree, I'm not saying "the standard doens't agree" or something like that. From my personal point of view, spread clearly states "copy" when used like [...array], while slice could mean you want a piece of an array, but when no params are given, it gives you the entire array.

Basically, I read array.slice() as JS does: "give me a portion of array, and that portion is from end to beggining", while I read [...array] as "give me a copy of array".

And while I agree is better to abstract those behaviors in functions, the code inside functions ideally should be super easy to read to anyone (mainly if you work with others). Taking that into consideration, I think using filter is clearer than using slice and splice.

And as I mentioned in my previous reply, your code doesn't do the same as mine, you need to add the findIndex logic to match, if you don't like the spread there, it still needs to look something like this:

const filterOut = filterer => source => {
  const copy = source.slice();
  const index = copy.findIndex(filterer);
  if (index >= 0) {
    copy.splice(index, 1);
  }
  return copy;
}

Which is still more complex than just doing filter. Finally: I dind't added the comments to explain the spread (is self explinatory), I added the comments to show you that there are 4 steps to achieve what you can achieve in just one.

Oh, I know you said you don't agree. And I don't agree. Like you said, your point of view.

Given that the definition of slice is to copy the array and should be used as such, I would expect it to be easier to understand, than say, using the spread operator, which is while is your preferred method, respectfully I disagree it's the clearest.

the filter method is used for a boolean comparison on condition and whilst it's a novel usage, is also not clear that it is being used to delete an item and would require inspection of code

Your original comment was about not using splice, I said it's fine. That's all I came here to say.

I mean, I appreciate you fancy use of currying to create ANY condition for deletion, but the original post was about deleting an object an index.

code of conduct - report abuse