0

I have an array of keys which I am interested in like

fillable = ['name', 'lastname', 'address' ]

and the request object can send any number of params

I want to create a new data object which consists of only variables represented by fillable array

Is there a better way to do this, without looping ? in ES6 or so

user1592129
  • 359
  • 1
  • 2
  • 12

1 Answers1

0

One option is to use reduce

let request = {name: 'John', lastname : 'Doe', address: 'bla bla', age : 67, gender : 'M' }
let fillable = ['name', 'lastname', 'address']

let newObj = fillable.reduce((c, v) => Object.assign(c, {[v]: request[v] || ""}), {});

console.log(newObj);
Eddie
  • 25,279
  • 6
  • 26
  • 53