鍍金池/ 問答/HTML/ for循環(huán)用函數(shù)式語言改寫

for循環(huán)用函數(shù)式語言改寫

我怎么把下面的代碼改寫成函數(shù)式語言,不用for循環(huán),直接用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,});
  }

回答
編輯回答
瘋浪

首先你要理解什么是函數(shù)式

其次map跟函數(shù)式沒有什么聯(lián)系

最后
map相關知識可以看一下
http://es6.ruanyifeng.com/#do...

函數(shù)式相關知識
https://www.zhihu.com/questio...

2017年12月8日 14:15
編輯回答
綰青絲
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: []
});
2018年9月4日 04:31
編輯回答
鹿惑
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,jtempArr都沒給,我就隨便寫一下reduce的處理。事實上沒啥必要,for循環(huán)效率挺高的

2017年11月15日 14:12