0

Trying to create an array out of existing one where all the consecutive items are merged:

[undefined, undefined, undefined] -> [undefined]

[undefined, 'item', undefined] -> [undefined, 'item', undefined]

[undefined, undefined, 'item'] -> [undefined, 'item']

I've tried [...new Set(temp)], where temp is the one of the above but that does not keep indexes.

EDIT: It would be beneficial if I could know number of items being merged into one when applicable

Please note the 2nd case where, despite duplicated items, array stays untouched

user0101
  • 1,277
  • 1
  • 9
  • 17

1 Answers1

2
  array.filter((el, i, a) => !i || el !== a[i - 1])
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Okay, this may work, but considering the single-letter variable names and the fact that the OP didn't think of this approach, could you explain what's going on here? – Cerbrus Oct 07 '19 at 09:44