ES6 map函数用不来,希望大佬可以帮忙用map帮忙转换一下这个if,想有个例子可以学习一下

for (let i = 0; i < this.dataSourecArry.length; i++) {
        if (this.dataSourecArry[i].checked) {
          if (this.dataSourecArry[i].label == "产废企业") {
            const ps = this.cfqy
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "经营企业") {
            const ps = this.jyqy
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "危险废物") {
            const ps = this.wastePs1
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "一般固废") {
            const ps = this.wastePs2
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "工业污泥") {
            const ps = this.wastePs3
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "医疗废物") {
            const ps = this.wastePs4
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "经营-危险废物") {
            const ps = this.wastePs5
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "经营-一般固废") {
            const ps = this.wastePs6
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          if (this.dataSourecArry[i].label == "经营-工业污泥") {
            const ps = this.wastePs7
            if (ps.length > 0) {
              this.addData(this.dataSourecArry[i].id, ps)
            }
          }
          console.log(this.dataSourecArry)
        } else {
          this.$refs.actionDate[
            this.dataSourecArry[i].id
          ].cesiumObject.entities.removeAll()
        }
      }

 

var array = [1,2,3]
function  fangfa(canshu){
    console.log(casnhu)
}
// 定义一个方法,这个方法需要一个参数。
array.map(fangfa)

// 你要的东西
function ceshi(item){
    if (item.checked) {
          if (item.label == "产废企业") {
            const ps = this.cfqy
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "经营企业") {
            const ps = this.jyqy
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "危险废物") {
            const ps = this.wastePs1
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "一般固废") {
            const ps = this.wastePs2
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "工业污泥") {
            const ps = this.wastePs3
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "医疗废物") {
            const ps = this.wastePs4
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "经营-危险废物") {
            const ps = this.wastePs5
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "经营-一般固废") {
            const ps = this.wastePs6
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          if (item.label == "经营-工业污泥") {
            const ps = this.wastePs7
            if (ps.length > 0) {
              this.addData(item.id, ps)
            }
          }
          console.log(this.dataSourecArry)
        } else {
          this.$refs.actionDate[
            item.id
          ].cesiumObject.entities.removeAll()
        }
}

this.dataSourecArry.map(ceshi)

 

你这个代码是选择性的调用this.addData()。
map函数是返回一个与原数组长度一致的新数组,并不适用你这个情况。
 

你这个也就是把for循环改成 this.dataSourecArry.forEach()
 

1.循环

let checkedList = dataSourecArry.forEach(d=>{
    if(d.checked){

        let ps = this.getPs(d);

       if(ps && ps.length > 0){

         this.addData(d.id, this.ps)

       }        

    }else{
        this.$refs.actionDate[
            d.id
          ].cesiumObject.entities.removeAll()
    }
}) ;

你这个获得ps的地方,肯定有关联的关系。要么都用一个变量,要么定义对应关系。比如label,或者其他主键或者code

const psObj = {

''医疗废物'':ps1,

"***":ps2,

}

this.getPs=({label})=>{

return psObj[label] ;

}

上面的this.addData(d.id, this.getPs(d)) ;

//2.先筛选再循环

let noCheckedList = dataSourecArry.filter(a=>!a.checked) ;

noCheckedList.removeAll操作;

let result = dataSourecArry.filter(a=>a.checked && this.getPs(a)).map(a=>({data:d,ps:this.getWPs(a)})) ;

这种方式循环次数会变多,看实际情况。

最主要的那个映射对象做出来。后续再有其他对象,循环代码不用调整,只需要