1

Following is my code which is giving Parsing error -

Code -

const { dataArr, anotherDataArr } = this.props.data
const myArr1 = dataArr.map(item => {'label': item.name,'value': item.code})
const myArr2 = anotherDataArr.map(item => { 'label': item.name, 'value': item.code})

this.setState({
    newArr1: myArr1,
    newArr2: myArr2,
})

myArr1, myArr2 should be an array of objects but I am getting parsing error. Let me know what silly mistake I am making here.

Nesh
  • 2,389
  • 7
  • 33
  • 54

1 Answers1

2

When you need to implicitly return an object from an arrow function wrap that object in (). Wrapping with () will make make the object expression. Without () {} will considered as block.

According to Returning object literals:

The code inside braces {} is parsed as a sequence of statements

Remember to wrap the object literal in parentheses().

const myArr1 = dataArr.map(item => ({'label': item.name,'value': item.code}))
const myArr2 = anotherDataArr.map(item => ({ 'label': item.name, 'value': item.code}))
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73