我怎么把下面的代码改写成函数式语言,不用for循环,直接用map操作可以吗,该怎么改,求大神指教 const tempData = {name: value[j].name};
const tempDragData = []; for (let k = 0; k < tempArr.length; k++) {
tempData[`col ${k}`] = tempArr[k].fieldName;
tempDragData.push({id: `item-${i++}`, name: value[j].name, content: tempArr[k].fieldName,});
}
2 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
const { tempData, tempDragData } = tempArr.reduce((obj, item, i)=>{
obj.tempData[`col ${k}`] = item.fieldName;
obj.tempDragData.push({ id: `item-${i++}`, name: value[j].name, content: item.fieldName,
}); return obj;
}, { tempData: { name: value[j].name
}, tempDragData: []
});
至尊宝的传说
TA贡献1789条经验 获得超10个赞
const tempData = {name: value[j].name};const tempDragData = [];
tempArr.reduce((p,c,k,a)=>{
tempData[`col ${k}`] = c.fieldName;
tempDragData.push({id: `item-${i++}`, name: value[j].name, content: c.fieldName,});
})i,j和tempArr都没给,我就随便写一下reduce的处理。事实上没啥必要,for循环效率挺高的
添加回答
举报
0/150
提交
取消
