0

How to use spread operator in implicit return wherein I'm removing one key value from the array of the object. I can use explicit return but I want shortcode and other possible solution of the scenario.


let array = [
{"sales":2341,"targetMet":false,"advertisment":true},
{"sales":981,"advertisment":true},
{"sales":3423,"targetMet":true,"advertisment":false},
{..},
{..}
];

let expectedArray = array.map(({targetMet,...rest}) => {...rest});

console.log(expectedArray) // should remove all targetMet keys 
  • What exactly is the problem you're facing? So far as I can see your code works fine (and to the best of my knowledge that's the succinctest way of achieving what you're asking). – Matt Apr 14 '19 at 18:28
  • 1
    `{...rest}` must be `({...rest})` .... but it should work otherwise? – Jonas Wilms Apr 14 '19 at 18:29

1 Answers1

0

Just return rest no need to spread it again.

Note: To return an object from arrow function wrap object in (). But here you don't need that.

let array = [
  {"sales":2341,"targetMet":false,"advertisment":true},
  {"sales":981,"advertisment":true},
  {"sales":3423,"targetMet":true,"advertisment":false},
];

let expectedArray = array.map(({targetMet,...rest}) => rest);

console.log(expectedArray)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73