Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript destructuring object containing fields with special characters

I have array (API response):

let arr = [
    { '@type': 'Something', data: 1234 },
    { '@type': 'Something', data: 3214 },
]

Is it possible to destructure elements with those '@' prefixed fields?

for (const { data, ??? @type } of arr) {}
like image 882
l00k Avatar asked Jan 10 '20 09:01

l00k


1 Answers

You could take a computed property and a new variable name.

let arr = [{ '@type': 'Something', data: 1234 }, { '@type': 'Something', data: 3214 }];

for (const { data, ['@type']: renamed } of arr) {
    console.log(renamed);
}
like image 87
Nina Scholz Avatar answered Nov 05 '22 11:11

Nina Scholz