我正在嘗試在組件數組中添加 id。id 將根據編號遞增。的組件。 component_names = ['test1', 'test2'];我想創建一個新的數組組件,它將變為:components = [{id:1, name: 'test1'}, {id:2, name: 'test2'}]// id 將隨著編號遞增。的組件增加。我在嘗試什么:let arr=[];for(let i=0;i<component_names.length;i++){ arr.push({id:i+1, name: component_names[i]});}上述解決方案有效,但我可以對 javascript 中的任何高階函數執行相同的操作嗎?
替換javascript中的for循環
慕哥9229398
2021-11-18 09:53:12