-1

I need to set one of the array property using arrow function. In my sample code I have added random key and an arrow function. but when I console the output, I cant see the "random" key. Can this be done using arrow functions.

let Allitems = {"items":[{"id":74489},{"id":64489},{"id":53489}]};

const newarr = Allitems.items.map(e => ({...e, name:"newnamme", random: (e) => { return e.id + "random"; } }));
 console.log(newarr);
// expected output
{"items":[ 
{"id":74489,name:"newnamme", random : "74489random"},
{"id":64489,name:"newnamme", random : "64489random"},
{"id":53489,name:"newnamme", random : "53489random"}]
}
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
Dhanesh Mane
  • 147
  • 11

1 Answers1

5

What you probably want is an immediately invoked function:

let Allitems = {"items":[{"id":74489},{"id":64489},{"id":53489}]};

const newarr = Allitems.items.map(e => ({
    ...e,
    name:"newnamme",
    random: ((e) => { return e.id + "random"; })(e)
}));
 console.log(newarr);
shaedrich
  • 5,457
  • 3
  • 26
  • 42