reduce用法--树状数据根据各级id,获得title值(数组
2019-08-10 本文已影响0人
前端青音
需求
![](https://img.haomeiwen.com/i11331125/80464d3d20583f87.png)
![](https://img.haomeiwen.com/i11331125/ddf9655b85c08638.png)
![](https://img.haomeiwen.com/i11331125/a765469ea2f3c981.png)
根据stageOption和stageOptionNow 得到activeStage:
stageOptionNow为当前选中的id,数组的第一项为第一层的id,数组的第2项为第2层的id,
现在要根据已知的id,获取对应级数的title值,即得到activeStage。
代码
computed: {
activeStage() {
let [firstId, secondId] = this.stageOptionNow || []
return this.stageOption.reduce(
(result, item) => {
if (item.id === firstId) {
result.first = item
result.second = item.child.find(child => child.id === secondId)
}
return result
},
{ first: {}, second: {} }
)
}
},