-1

is it possible simplify .map's return statement? I want to add key for each element in the array, and transform arr to array of object. thanks!

let arr = ['c:/a.txt', 'b:/c.txt', 'd:/e.txt'];
let arrObj = arr.map((file) => return {path: file});
0day
  • 103
  • 1
  • 10

1 Answers1

2

You could use a short hand property.

let arr = ['c:/a.txt', 'b:/c.txt', 'd:/e.txt'],
    arrObj = arr.map(path => ({ path }));

console.log(arrObj);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392