Vue.Js-如何解析整数数组对象数据

时间:2018-08-11 03:47:24

标签: arrays parsing vuejs2 integer

我在数组对象中有一个响应数据

这是我的回复代码:

{
  "data": [
    {
      "id": 7,
      "product_name": "iPad",
      "rating": "4.00"
    },
    {
      "id": 2,
      "product_name": "iPhone",
      "rating": "3.00"
    },
    {
      "id": 8,
      "product_name": "iPod",
      "rating": "7.00"
    },
    {
      "id": 4,
      "product_name": "iMac",
      "rating": "9.00"
    }
  ],
  "status": 200,
  "error": 0
}

然后我尝试映射数组对象以将数据“定级”解析为整数,这里是我的axios代码:

getCustomerType: function(store) {
  this.loading = true
  let headers = {
    Authorization: 'Bearer ' + window.accessToken
  }

  axios({
    method: 'GET',
    url: baseApi(this.selectedStore.url_id, 'en', 'customertype'),
    params: this.params,
    headers: headers
  })
    .then(response => {
      this.customerTypeData = response.data.data
      for (let index = 0; index < this.customerTypeData.length; index++) {
        this.customerTypeData.rating[index] = parseInt(this.customerTypeData.rating[index])
      }
      this.loading = false
    })
}

但是parseInt的代码并不能解决

for (let index = 0; index < this.customerTypeData.length; index++) {
        this.customerTypeData.rating[index] = parseInt(this.customerTypeData.rating[index])
      }

预期

(按索引)评分,按索引数组从字符串更改为数字

0 个答案:

没有答案