js 第二次赋值为空?

代码如下:

methods: {

submit(){

this.tableData.forEach(sub => {

sampleProcessList = sub.sampleProcessInfo.sampleProcessList

})

let sampleProcessListss = JSON.parse(JSON.stringify(sampleProcessList))

sampleProcessListss.find(element => {

if (element.id === this.input.selectSampleProcess) {

this.processName = element.processName

}

})

const idList = this.ids.map(i => i)

this.tableData.forEach(v => {

if (idList.includes(v.id)) {

v.processName = this.processName

}

})

console.log(this.processName);

}

}

问题如下
js 第二次赋值为空?
期望每次提交 得到的值 是不同的,谢谢大佬们


回答:

this.tableData.forEach(sub => {

sampleProcessList = sub.sampleProcessInfo.sampleProcessList

})

等价于

sampleProcessList = this.tableData[this.tableData.length - 1]?.sampleProcessInfo.sampleProcessList

let sampleProcessListss = JSON.parse(JSON.stringify(sampleProcessList))

sampleProcessListss.find(element => {

if (element.id === this.input.selectSampleProcess) {

this.processName = element.processName

}

})

等价于

this.processName = sampleProcessList.find(v=>v.id === this.input.selectSampleProcess)?.processName

      const idList = this.ids.map(i => i)

this.tableData.forEach(v => {

if (idList.includes(v.id)) {

v.processName = this.processName

}

})

console.log(this.processName);

}

等价于没写,毕竟没有 this.processName 赋值


最后就看前两句就行了,

// 获取表格中最后一行的 sampleProcessInfo.sampleProcessList

sampleProcessList = this.tableData[this.tableData.length - 1]?.sampleProcessInfo.sampleProcessList

// 从 sampleProcessList 找到和 selectSampleProcess 的第一条数据的 processName

this.processName = sampleProcessList.find(v=>v.id === this.input.selectSampleProcess)?.processName


回答:

问题是你的第一个循环出问题了
this.tableData.forEach(sub => {

sampleProcessList = sub.sampleProcessInfo.sampleProcessList

})
这个不是把tableData中所有元素的sub.sampleProcessInfo.sampleProcessList是对象,你需要赋值给sampleProcessList,你现在的处理逻辑是循环过程中,是每个都会重新赋值一次,直到循环结束,你sampleProcessList拿到数据其实就是tableData的最后一个元素的数据,如果所有的sub.sampleProcessInfo.sampleProcessList是对象,你需要sampleProcessList.push(sub.sampleProcessInfo.sampleProcessList),如果是数组,sampleProcessList=sampleProcessList.concat(sub.sampleProcessInfo.sampleProcessList)

以上是 js 第二次赋值为空? 的全部内容, 来源链接: utcz.com/p/934284.html

回到顶部