2

I am wanting to use map to create a new array of of objects that have two properties from the original objects in the originating array, so I am trying this:

const filteredConfigurableModules = configurableModules.map(module =>
  module.name, module.configurables
);

But this isn't working, as only the "name" is saved to the new array. What am I missing here?

I also tried this:

const filteredConfigurableModules = configurableModules.map(module => {
  name: module.name,
  configurables: module.configurables
});

... but end up with a syntax error:

SyntaxError: Unexpected token ':'

5
  • 3
    configurableModules.map(module =>({ name: module.name, configurables: module.configurables })); Commented Dec 17, 2019 at 15:20
  • If you want to return an object, enclose it in {}. Try configurableModules.map(module =>({ module.name, module.configurables })). Commented Dec 17, 2019 at 15:21
  • SyntaxError: Unexpected token ':' Commented Dec 17, 2019 at 15:27
  • const mapped = [{id: 1, data: 'thing'}, {id: 2, data: 'thing'}].map(item => ({ id: item.id * 10, data: item.thing + ' another thing' })). This works. just tried it, which means the syntax Andrey provided is valid Commented Dec 17, 2019 at 15:30
  • See Siddharth's answer below: the key was the extra parentheses around the object. Commented Dec 17, 2019 at 15:31

4 Answers 4

5

As I see you want to map from one array to another. You can do it in 2 ways:

Inline

  const filteredConfigurableModules = configurableModules.map(module => ({name: module.name, configurables: module.configurables}));

When you do inline and return an object you have to wrap it in parenthesis so that it gets evaluated properly.

Use Return Statement

const filteredConfigurableModules = configurableModules.map(module => {
  return {
    name: module.name,
    configurables: module.configurables
  };
});
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain a little?
This worked. Thanks. Key was the extra parentheses around the object.
this is because the shorthand fat arrow and returning an object dont go well together. runtime cant tell if youre trying to use a function block or an object.
1

You should wrap it with parentheses as follow:

const filteredConfigurableModules = configurableModules.map(({name, configurables})=> ({
  name,
  configurables
}));

Comments

0

You can create a new object on the arrow function from the map:

const filteredConfigurableModules = configurableModules.map(module =>
  ({ name: module.name, configurables: module.configurables })
);

this return an array of objects with name and module fields.

If you return an object, you need to use parenthesis because otherwise it will be treated as a code body with no return.

2 Comments

this is because the shorthand fat arrow and returning an object dont go well together. runtime cant tell if youre trying to use a function block or an object.
Just wrap the object in parentheses.
0

Can you try this if i understand your question correctly

const filteredConfigurableModules = configurableModules.map(module => ({
  name : module.name,
  configurables : module.configurables,
}));

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.