marcXandre
marcXandre

Reputation: 2432

How to map on a array and return a object with tslint. (syntactic sugar)

This is a pure syntactic sugar question.

How can I iterate over a array with map and return a new object without having TSLint saying:

This arrow function body can be simplified by omitting the curly braces and the keyword 'return', and wrapping the object literal in parentheses.

For example, the object user:

class User {
    constructor(
        public id: number, 
        public first_name: string, 
        public last_name: string, 
        public gender: Date, 
        public location: number, 
    )
}

And when I do this :

const simple_users = users.map(u => { return { name: u.name, id: u.id} });

Then this happens :

[tslint] This arrow function body can be simplified by omitting the curly braces and the
keyword 'return', and wrapping the object literal in parentheses. (arrow-return-shorthand)

And I want to keep the tslint rule arrow-return-shorthand.

Upvotes: 0

Views: 2756

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

Simply wrap your object inside ()(parenthesis) and remove the function and return statement. The shorthand is below.

const simple_users = users.map(u => ({ name: u.name, id: u.id}));

Further destructuring version would be more shorten.

const simple_users = users.map(({name, id}) => ({ name, id}));

Upvotes: 8

Related Questions