【问题标题】:Pass data json string to number Vue.js将数据 json 字符串传递给数字 Vue.js
【发布时间】:2020-08-09 06:07:52
【问题描述】:

我正在尝试按数字和通过 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
    }
]

【问题讨论】:

    标签: javascript json vue.js vuejs2 axios


    【解决方案1】:

    尝试使用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);});
    

    【讨论】:

    • 非常感谢!这就是答案,.catch () 之前唯一缺少的 })
    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多