Reputation: 8597
I would like to know if it possible to deconstruct (in my case x
) an array and create an alias in order to avoid issues with reserved keywords.
Here an example, but it gives me an error. Any idea how to solve it? Thanks
const mk = (data) =>
data.map((x, idx) => {
const [a, b, for:xxx, d] = x
return {
a,
b,
for:xxx, // I want create an object with for property name
d
}
})
Upvotes: 0
Views: 4413
Reputation: 23575
You are mixing up objects and arrays. You are tying to use an alias on a array deconstruction.
If you deconstruct an array, there is no need for an alias
const x = [
'a',
'b',
'd',
'for',
];
const [
a,
b,
d,
xxx,
] = x;
console.log({
a,
b,
xxx,
d
});
If you are deconstructing an object, using for
isn't a problem.
const x = {
a: 'a',
b: 'b',
d: 'd',
for: 'for',
};
const {
a,
b,
for: xxx,
d,
} = x;
console.log({
a,
b,
xxx,
d
});
Upvotes: 3
Reputation: 6250
In the context of an array, deconstructing for: xxx
does not make any sense. This only works for an object having a for
property (const {a, b, for: xxx, d } = x;
).
xxx
is simply the third element of the given array.
The following might do it:
const mk = (data) =>
data.map((x: any, idx: any) => {
const [a, b, c, d] = x;
return {
a,
b,
for: c, // I want create an object with for property name
d,
}
})
Upvotes: 1