1
data.map(obj => 
        {person_name: obj.user.name,
        ...obj})

Why above code failed?

my data look like this

[{user:{name:'hello'},age:1},{user:{name:'world'},age:1,{user:{name:'another_name'},age:1]

I want to 'pull' the obj.user.name out from the nested obj.

4
  • what is ...obj doing? please add the wanted result as well. Commented Jun 9, 2017 at 7:40
  • @NinaScholz the other property? Commented Jun 9, 2017 at 7:50
  • 1
    Note that ... is a punctuator used in a number of different contexts (e.g. spread element, assignment rest element), it's not an operator. Commented Jun 9, 2017 at 7:52
  • You should have explained in which way it failed. We can't tell whether you know that you are using experimental features and have set up the build chain accordingly, or if that's the problem, Commented Jun 9, 2017 at 13:13

5 Answers 5

2

Try this:

data.map(obj => ({person_name: obj.user.name,...obj}))

The { at the beginning of the object was interpreted as the beginning of a block instead of an object.

You can read more about this on MDN

EDIT: As Pawel mentioned, using the spread operator with objects does not work with pure es6 you need to use a specific transform like in this proposal: https://github.com/tc39/proposal-object-rest-spread

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

1 Comment

@pawel I assumed he was transpiling using the object spread transform proposed for es7. Otherwise the problem wouldn't be map specific.
1

You can't spread an object like this (throws SyntaxError at ....

I would write it using Object.assign:

data.map(obj => Object.assign({person_name: obj.user.name}, obj ));

3 Comments

What presets do you use to transpile? babeljs with es2017 still throws syntax error
It isn't ES2017 @pawel, object rest/spread is a proposal, it isn't part of the language: github.com/tc39/proposals Here is the babel transform you need to use if you want to be able to transpile: babeljs.io/docs/plugins/transform-object-rest-spread
@DanCouper I know, that's why I have asked for the details. For me it's a syntax error until it makes it to the official language spec (or, if one uses experimental features, it should be stated in the question).
0

With babel, you could use rest syntax (parameters) ... for getting the rest of the object and store name property directly.

var data = [{ user: { name: 'hello' }, age: 1 }, { user: { name: 'world' }, age: 1 }, { user: { name: 'another_name' }, age: 1 }];

console.log(data.map(({ user, ...obj }) => Object.assign(obj, { person_name: user.name })));
.as-console-wrapper { max-height: 100% !important; top: 0; }

4 Comments

You don't seem to be using rest parameters here?
@FelixKling, does ...obj count?
Since that's in an object destructuring it would be a "rest property". Rest parameter would be function foo(...bar) {} which collects all arguments passed to foo. In your example, the callback will always only handle a single argument.
@FelixKling, the description on MDN is a bit hidden, but i changed the link.
0

First of all, if you want to create an object using an arrow function, you have to wrap the curly braces in parenthesis:

data.map(obj => ({ foo: 'bar' }));

Otherwise, JS thinks the curly braces are the function body, and you get a syntax error.

And secondly, you can't use the spread operator in object literals. You can only use it when calling a function, using the array literal or when destructuring.

Comments

0

You can use Array.prototype.map() and directly return the desired object:

const data = [{user: {name: 'hello'},age: 1}, {user: {name: 'world'},age: 1},{user: {name: 'another_name'},age: 1}];
const result = data.map(obj => {
  return {
    person_name: obj.user.name,
    age: obj.age
  };
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.