0

I'm using .map() to copy object fields from one JSON List to another, my issue is that the starting list I need to move the values from has spaces in the field names.

For fields with no spaces the following is working:

$scope.dataList = results.data.map(el => ({
     IdApplication: el.ApplicationReference,
     AccNo: el.AccountNumber
}))

The starting JSON however is taken directly from a CSV the user selects, and sadly the client won't change how they name their columns. So I need to map a JSON field called 'Application Reference'.

I've tried enclosing the field name in '' but this gives an identifier expected error on the first opening ' .

Bracket notation also doesn't work:

$scope.dataList = results.data.map(el => ({
    IdApplication: el.['Application Reference'],
    AccNo: el.AccountNumber
}))

This also gives an error of identifier expected on the opening [.

2 Answers 2

2

You said bracket notation doesn't work but your example doesn't show usage of bracket notation.

The correct usage would be:

$scope.dataList = results.data.map(el => ({
    IdApplication: el['Application Reference'],
    AccNo: el['Account Number']
}))

P.S. It's considered good practice to use camelCase for JSON properties https://google.github.io/styleguide/jsoncstyleguide.xml#Property_Name_Format

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to update the second section of code, i've updated it now, and can see that I was doing it wrong, I was leaving the period in after "el" then doing the bracket notation. e.g. el.['Application Reference']
1

You can use this code

$scope.dataList = results.data.map(el => {
    IdApplication: el['Application Reference'],
    AccNo: el['Account Number']
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.