[英]How to assign value of array element by a arrow function
I need to set one of the array property using arrow function.我需要使用箭头 function 设置数组属性之一。 In my sample code I have added random key and an arrow function.
在我的示例代码中,我添加了随机键和箭头 function。 but when I console the output, I cant see the "random" key.
但是当我控制台 output 时,我看不到“随机”键。 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"}]
}
What you probably want is an immediately invoked function :您可能想要的是立即调用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.