JavaScript:JSON 循環遍歷相似的鍵名

羅布·吉布森

對編程非常陌生,我正在做一些在線教程。希望有好心人幫幫我。

我所擁有的是來自 API 響應的以下 JSON 數據。即使沒有值, strOption[i] 和 strPrice[i] 也會上升到 10,所以我想擺脫它們。

格式並不完美,但我相信你能看懂。

cars {
     "car": [
         0 : {
           "carId": "17209",
           "strModel": "Discovery",
           "strYear": "2022",
           "strOption1": "S",
           "strOption2": "R-D S",
           "strOption3": "R-D HSE",
           "strOption4": "",
           "strPrice1": "68,600",
           "strPrice2": "71,100",
           "strPrice3": "85,400",
           "strPrice4": ""
         }
         1 : {
           "carId": "11349",
           "strModel": "Sport",
           "strYear": "2022",
           "strOption1": "SE",
           "strOption2": "HSE",
           "strOption3": "HST",
           "strOption4": "",
           "strPrice1": "82,200",
           "strPrice2": "93,700",
           "strPrice3": "98,100",
           "strPrice4": ""
         }
      ]
   }

我想要做的是遍歷數據並最終得到這個。
*注意 strOption1 必須與 strPrice1 配對,以便我可以訪問 strOptions[0] 和 strPrices[0] 等。

cars {
     "car": [
         0 : {
           "carId": "17209",
           "strModel": "Discovery",
           "strYear": "2022",
           "strOptions": ["S", "R-D S", "R-D HSE"],
           "strPrices": ["68,600", "71,100", "85,400"]
         }
         1 : {
           "carId": "11349",
           "strModel": "Sport",
           "strYear": "2022",
           "strOptions": ["SE", "HSE", "HST"],
           "strPrices": ["82,200", "93,700", "98,100"]
         }
      ]
   }
阿卜杜洛赫·穆哈馬喬諾

您可以使用Object.prototype.keys,Array.prototype.forEach()和 的組合String.prototype.startsWith()來獲得所需的功能。

const cars = {
     "car": [
         {
           "carId": "17209",
           "strModel": "Discovery",
           "strYear": "2022",
           "strOption1": "S",
           "strOption2": "R-D S",
           "strOption3": "R-D HSE",
           "strOption4": "",
           "strPrice1": "68,600",
           "strPrice2": "71,100",
           "strPrice3": "85,400",
           "strPrice4": ""
         }, 
         {
           "carId": "11349",
           "strModel": "Sport",
           "strYear": "2022",
           "strOption1": "SE",
           "strOption2": "HSE",
           "strOption3": "HST",
           "strOption4": "",
           "strPrice1": "82,200",
           "strPrice2": "93,700",
           "strPrice3": "98,100",
           "strPrice4": ""
         }
      ]
   }
   
   const result = cars.car.map(el => {
       let details = { // default object
           "strOptions": [],
           "strPrices": []
       }
       Object.keys(el).forEach(key => {
          if(key.startsWith('strOption')){
              if(el[key]) // prevent ""
                  details.strOptions.push(el[key])
          } else if(key.startsWith('strPrice')) {
              if(el[key]) // prevent ""
                  details.strPrices.push(el[key])
          } else {
              details[key]=el[key]
          }
       })
       
       return details
   })
   
   console.log(result)
   
   

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章