Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destructure an array with object that has space in its keys? [duplicate]

data={data.map(({ ID,filePath,accountId,companyId,['First Name'], ...rest }) => rest)}

In this case First Name is a key with space, apparently when passed as above it causes error. How to handle this scenario?

like image 727
Joji Thomas Avatar asked Dec 13 '22 09:12

Joji Thomas


1 Answers

Variable names (identifiers) can't have spaces in them, you won't be able to destructure that property into a standalone variable unless you also rename the variable - which can be done using bracket notation:

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);
like image 137
CertainPerformance Avatar answered Dec 28 '22 12:12

CertainPerformance