Rest parameter to catch a subarray

you cannot use the rest parameter to catch a subarray that leaves out the last element of the original array

any example plz?
my I am unable to get the context or angle of thought of the writer here,
#german:programming-help

Can we get some context of where this comes from?

Also, how do we provide an example of us “not” doing something?

Look at it this way…

There is the output of an assembly line in front of you. There are cookies coming off it - you don’t know how many, but you know it will be between 5 and 10. You say, "The first one goes to my daughter Alice, the second one to my son Bob, the third to my daughter Carol, and just throw the rest in a bag.

That bag is the “rest”. It is whatever is leftover. The quote is saying that you can’t say, “throw the rest except the last one in the bag”. All you can say is “throw the rest in a bag”.

its the exercise, in which this line was given for some guideline or ethics of rest operator in es6 practices

just to add on to what kevin was saying.

this will just add items, it wont modify the last one:

const items = [
  [1, "apples"],
  [5, "oranges"],
  [7, "brownies"],
];

const increasedBrowniesCount =[...items, [8, "brownies"]]

if you want to only modify the last subarray say for example increase the count you can do this:

const items = [
  [1, "apples"],
  [5, "oranges"],
  [7, "brownies"],
];

const increasedBrowniesCount = items
  .slice(0, items.length - 1)
  .concat([[items[items.length - 1][0] + 1, items[items.length - 1][1]]]);

if you wanted to create a function that increases a count of a given item of such an array you would do this:

const increaseItemCount = (items, name) => {
  const newItems = items.map((item) => {
    if (item[1] === name) {
      return [item[0] + 1, ...item.slice(1)];
    } else {
      return item;
    }
  });
  console.log(newItems);
};

increaseItemCount(items, "oranges");

but this wont work it will just add and wont replace the count like you want, like the author was mentioning:

const increaseItemCount = (items, name) => {
  const newItems = items.map((item) => {
    if (item[1] === name) {
      return [item[0] + 1, ...item];
    } else {
      return item;
    }
  });
  console.log(newItems);
};
1 Like

yes like kevin said, the author’s language is confusing but i think he means

you cannot use the rest parameter to catch a subarray that leaves out the last element of the original array

the rest parameter performs a shallow copy until the end, its like slicing until the end… however if you want to use spread with destructuring and leave out the first two elements you can do as the author shows in his example, however the opposite:

const [...arr, y, z] = [1, 2, 3, 4, 5, 6, 7, 8];

will not work.
as the rest element must be last in a destructuring assignment. does that make sense?

1 Like

The quote in question is saying you can do this:

const [first, second, ...theRest] = arr

but you can’t do this:

const [first, second, ...theRest, last] = arr

It always grabs the end, never the beginning or middle.

2 Likes

Wouldn’t really be rest if you had something after it. You can nest them though (not sure I can remember seeing a good use of it).

const [a, b, ...[c, d, ...[e, f]]] = [1, 2, 3, 4, 5, 6];
console.log(a, b, c, d, e, f); // 1 2 3 4 5 6

I guess a language might support some form of “gather operation” but it wouldn’t be a rest property/element then.

Imaginary code

const [first, ...gather, last] = [1, 2, 3, 4, 5]

first // 1
gather // [2, 3, 4]
last // 5

got it,
and it was totally understandable, but I was missing the ''Leaving out the last element of the array ‘’ thing and its clear now