美文网首页
2018-10-03

2018-10-03

作者: Panco_Lee | 来源:发表于2018-10-03 20:31 被阅读0次

多维数组转一维

let list = [
       {
         "id":31,
         "type":3,
         "name":"颜色",
         "sysName":"defaultSysName",
         "valueType":1,
         "list":["红色" ,"黄色" ,"绿色"]
       },
       {
         "id":33,
         "type":3,
         "name":"尺寸",
         "sysName":"defaultSysName",
         "valueType":1,
         "list":["X" ,"XX" ,"XXL"]
       },
       {
         "id":40,
         "type":3,
         "name":"大小",
         "sysName":"defaultSysName",
         "valueType":1,
         "list":["1CM" ,"2CM" ,"3CM"]
       }
     ]

     list = list.map((itemList) => {
       itemList["list"] = itemList["list"].map((item) => {
         item = {fieldValue: `${itemList.id}=${item}` ,name:`${itemList.name}:${item}`}
         return item
       })
       return itemList
     })
     let firstList = list.shift()["list"]
     let options = []
     if (firstList.length > 0) {
       let listItem = list.shift()["list"]
       while (listItem) {
         firstList.forEach((firstItem) => {
           listItem.forEach((item) => {
             let option = firstItem.fieldValue
             option = `${option}&${item.fieldValue}`
             let name= firstItem.name
             name= `${name}  ${item.name}`
             options.push({fieldValue: option ,name:name })
           })
         })
         firstList = options
         options = []
         if(list.length > 0){
           listItem = list.shift()["list"]
         }else {
           listItem = undefined
         }
       }
       console.log(firstList)
     }
     

结果log

Jietu20181003-203030.png

相关文章

网友评论

      本文标题:2018-10-03

      本文链接:https://www.haomeiwen.com/subject/okltaftx.html