简体   繁体   中英

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',
                }
            })
        ];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM