0

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',
                }
            })
        ];
1
  • Can you please elaborate what you're trying to do, how does dictionary look like? The problem is obvious as the data will be an array that contains a sub-array as map will return an array, but I don't understand what you're trying to achieve from this as dictionary looks like an array of objects, so why would you iterate through it again, is it just to get some specific keys? otherwise just removing brakets from your implementations + change first curly brakets to parenthesis will do the job. Cheers Commented Oct 10, 2020 at 19:58

1 Answer 1

3

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.

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

4 Comments

this does not solve my problem as it indicates a colon error, in the object
The code in my answer is working ok. Perhaps your error is originating elsewhere.
@MyGuitar can you show the error you're getting using the code above?
Thanks for the answer. If enclosed in parentheses, the syntax error goes away

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.