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
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);
};
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?
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.
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
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.