1

In the below example, unmapped array items must be mapped to rest parameter in the left hand side, but for some reason the output is different,

var planets = ["Mercury", "Earth", "Venus", "Mars", "Pluto", "Saturn"];
var [first, second, ...rest] = ["Mercury", "Earth", ...planets, "Saturn"];

console.log(first); //Output: Mercury
console.log(second); //Output: Earth

Now for the below, expected output is,

console.log(rest); //Output: ["Venus", "Mars", "Pluto", "Saturn"]

But the actual output is,

console.log(rest); //Output: "Mercury", "Earth", "Venus", "Mars", "Pluto", "Saturn", "Saturn"

Source:https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/

Whats happening here?

2
  • That's now how spread syntax and rest syntax works, Commented Apr 5, 2020 at 11:47
  • Does this answer your question? Spread Syntax ES6 Commented Apr 5, 2020 at 12:02

3 Answers 3

2

When you spread planet into your your second array, you're putting all the elemtns from the planets array into the second array, so:

["Mercury", "Earth", ...planets, "Saturn"];

... evaluates to:

["Mercury", "Earth", "Mercury", "Earth", "Venus", "Mars", "Pluto", "Saturn", "Saturn"];

you then destructure the first and second elements from this array giving you "Mercury" and "Earth". You then use the rest pattern ...rest to retrieve the remaining elements (ie elements from index 2 and onwards) and store them in the array named rest. Thus, your rest array contains all the elements from the array above, excluding the first and second elements:

["Mercury", "Earth", "Venus", "Mars", "Pluto", "Saturn", "Saturn"]

To do get your expected output, you can destucture the first array, by ignoring the first two elements:

const [,,...rest] = ["Mercury", "Earth", "Venus", "Mars", "Pluto", "Saturn"];
console.log(rest);

Sign up to request clarification or add additional context in comments.

1 Comment

@Nag I see, no worries
1

You can use Set()

The Set object lets you store unique values

var planets = ["Mercury", "Earth", "Venus", "Mars", "Pluto", "Saturn"];
var [first, second, ...rest] = new Set(["Mercury", "Earth", ...planets, "Saturn"]);

console.log(first);
console.log(second);

console.log(rest);

Comments

1

You can spread selectively. When you spread ...planets it copies all the values from planets array to the array you're creating.

You can simply use slice before spreading.

var planets = ["Mercury", "Earth", "Venus", "Mars", "Pluto", "Saturn"];
var [first, second, ...rest] = ["Mercury", "Earth", ...planets.slice(2, planets.length - 1), "Saturn"];

console.log(first);
console.log(second);
console.log(rest)

2 Comments

FYI. Saturn appears twice
@User863 we can avoid that just simply using second argument of slice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.