如何使用键中包含 space 的对象解构数组?
How to destructure an array with object that has space in its keys?
data={data.map(({ ID,filePath,accountId,companyId,['First Name'], ...rest }) => rest)}
在这种情况下,名字是 space 的键,显然当按上述方式传递时会导致错误。如何处理这种情况?
变量名(标识符)中不能有空格,您将无法将 属性 解构为独立变量 除非 您也重命名变量 - 可以使用括号表示法完成:
data.map(({
ID,
filePath,
accountId,
companyId,
['First Name']: firstName,
...rest
}) => rest)
const data = [
{
'First Name': 'foo',
'anotherProp': 'another'
},
{
'First Name': 'bar',
'anotherProp': 'another'
}
];
const mapped = data.map(({
ID,
filePath,
accountId,
companyId,
['First Name']: firstName,
...rest
}) => rest);
console.log(mapped);
data={data.map(({ ID,filePath,accountId,companyId,['First Name'], ...rest }) => rest)}
在这种情况下,名字是 space 的键,显然当按上述方式传递时会导致错误。如何处理这种情况?
变量名(标识符)中不能有空格,您将无法将 属性 解构为独立变量 除非 您也重命名变量 - 可以使用括号表示法完成:
data.map(({
ID,
filePath,
accountId,
companyId,
['First Name']: firstName,
...rest
}) => rest)
const data = [
{
'First Name': 'foo',
'anotherProp': 'another'
},
{
'First Name': 'bar',
'anotherProp': 'another'
}
];
const mapped = data.map(({
ID,
filePath,
accountId,
companyId,
['First Name']: firstName,
...rest
}) => rest);
console.log(mapped);