Câu hỏi phỏng vấn Javascript
Câu hỏi

Suggest one simple way of removing duplicates from an array using ES6

Câu trả lời

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:

javascript Copy
function removeDuplicates(array) {
    return [...new Set(array)];
}

Explanation

  • 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

middle

Gợi ý câu hỏi phỏng vấn

senior

Check if a given string is a isomorphic

middle

What is a closure, and how/why would you use one?

entry

What is Scope in JavaScript?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào