Languages
[Edit]
EN

JavaScript - remove duplicated lines

3 points
Created by:
Iona
445

Hello there! πŸ‘‹ 😊

In this article, I would like to show you two ways to remove duplicated lines from some text in JavaScript.

We're gonna use two methods: 

  • filter()
  • reduce()

1. filter() based example

Edit

This approach uses a functional programming pattern.

On the text I've used set of operations to remove duplicated lines:

  • split() method to split text into lines that take a newline character in 4 different variants which is a universal approach,
  • filter() method that creates new array of elements tested with provided function
  • join() method to merge items back.

Note:
You can read more about splitting text in this article.

Practical example:

2. reduce() based example

Edit

This approach was created to show that it is possible to get the same effect as in the example above with reduce() method.

Practical example:

References

Edit
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - Blog posts

Native Advertising
πŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

β€οΈπŸ’» πŸ™‚

Join