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:

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

How can you share code between files?

middle

Could you explain the difference between ES5 and ES6

expert

Is it possible to reset an ECMAScript 6 generator to its initial state?

Bình luận

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

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