We can ignore a value while destructing a list:
[a,,...rest] = [1,2,3,4] //2 is ignored here
console.log(a); // 1
console.log(rest); // [3,4]
Can we do something similar for dictionaries? I tried, but it gives error:
({ a, , c} = { a: 10, b: 20, c: 30 }); //Unexpected token ',
/* I was expecting a to get assigned 10 and c to get assigned 30 */
Is it simply impossible to do the same with dictionaries? If yes, then why such a language design decision? Or it's just the parser incapability?
Here is how to do it:
({a,c} = { a: 10, b: 20, c: 30 })
console.log(a)
console.log(c)
The first one works that way because it's a spread in then end.
The second one sees it as a trailing comma.
You can just do that.
({ a, c} = { a: 10, b: 20, c: 30 });
console.log(a);
console.log(c);
Yes you can ignore the key while destructing the dictionaries/object like below :-
( { a, ...remObj} = { a: 10, b: 20, c: 30 });
console.log(a, ' ',remObj);