Suggest one simple way of removing duplicates from an array using ES6
Suggest one simple way of removing duplicates from an array using ES6
One simple and effective way to remove duplicates from an array using ES6 (ECMAScript 2015) is by utilizing the Set
object along with the spread operator. Here's how you can do it:
function removeDuplicates(array) {
return [...new Set(array)];
}
Set Object: The Set
is a built-in object introduced in ES6 that lets you store unique values of any type. When you pass an array to the Set
constructor, it automatically removes any duplicate values because a Set
is designed to only store unique elements.
Spread Operator: The spread operator (...
) is used to expand elements of an iterable (like an array or string) into individual elements. In this case, it is used to convert the Set
back into an array.
This method is not only concise but also efficient, as the creation of a set from an array typi...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào