My Guitar
My Guitar

Reputation: 17

Syntax error while iterating over data in an array

I want to iterate over the data and place the resulting objects in an array in the "data" array. With this implementation, I get a syntax error. What am I doing wrong? Is my approach correct?

const data = [
            dictionary.map(item => {
                {
                    key: item.id,
                    engword: item.title,
                    rusword: item.body,
                    tags: 'beginner',
                }
            })
        ];

Upvotes: 0

Views: 35

Answers (1)

domenikk
domenikk

Reputation: 1743

The .map() method returns an array, so you should assign it's value to data variable like this:

const data = dictionary.map(item => (
                {
                    key: item.id,
                    engword: item.title,
                    rusword: item.body,
                    tags: 'beginner',
                }
            ));

Also, you have double curly braces in your arrow function. When returning an object literal like that, you should wrap it in parentheses.

Upvotes: 3

Related Questions