首页 > 解决方案 > 将数据 json 字符串传递给数字 Vue.js

问题描述

我正在尝试按数字和通过 API 收集的单词对表格进行排序,单词对它们进行了很好的排序,但数字没有,如何将字符串的值传递给数字。

这是我对 API 的调用。

axios.get(`/json/secciones`+ tienda +`.json`)
      .then(response => {this.userInfo = response.data;})
      .catch(e => {this.errors.push(e);});

并把它还给我

[
    {
      "id_store": 2,
      "id_section": 1,
      "desc_section": "MATERIALES DE CONSTRUCCION",
      "id_rule": 1,
      "sale_potential": "79413.5525190617"
    },
    {
      "id_store": 2,
      "id_section": 2,
      "desc_section": "CARPINTERIA Y MADERA",
      "id_rule": 1,
      "sale_potential": "74704.3439572555"
    },
    {
      "id_store": 2,
      "id_section": 3,
      "desc_section": "ELECTR-FONTAN-CALOR",
      "id_rule": 1,
      "sale_potential": "101255.89182774"
    },
    {
      "id_store": 2,
      "id_section": 4,
      "desc_section": "HERRAMIENTA",
      "id_rule": 1,
      "sale_potential": "36969.8901028374"
    }
    ]

我怎么能像这样找回它?

[
    {
      "id_store": 2,
      "id_section": 1,
      "desc_section": "MATERIALES DE CONSTRUCCION",
      "id_rule": 1,
      "sale_potential": 79413.5525190617
    },
    {
      "id_store": 2,
      "id_section": 2,
      "desc_section": "CARPINTERIA Y MADERA",
      "id_rule": 1,
      "sale_potential": 74704.3439572555
    },
    {
      "id_store": 2,
      "id_section": 3,
      "desc_section": "ELECTR-FONTAN-CALOR",
      "id_rule": 1,
      "sale_potential": 101255.89182774
    },
    {
      "id_store": 2,
      "id_section": 4,
      "desc_section": "HERRAMIENTA",
      "id_rule": 1,
      "sale_potential": 36969.8901028374
    }
]

标签: javascriptjsonvue.jsvuejs2axios

解决方案


尝试使用map应用于数组的函数并使用Number()对象构造函数将该属性转换为数字:

axios.get(`/json/secciones`+ tienda +`.json`)
      .then(response => {this.userInfo = response.data.map(item=>{
                                     item.sale_potential=Number(item.sale_potential)
                                          return item;
                                      });

})
      .catch(e => {this.errors.push(e);});

推荐阅读