javascript - 将数据 json 字符串传递给数字 Vue.js

标签 javascript json vue.js vuejs2 axios

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

最佳答案

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

关于javascript - 将数据 json 字符串传递给数字 Vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61429189/

相关文章:

javascript - React-spring 动画仅在第一次渲染时起作用

javascript - JSON - JavaScript 获取嵌套值

css - 如何使用 Vue 渲染 CSS 网格的标记?

php - 如何使用 Axios 将图像从 VueJS 上传到 Symfony?

vue.js - 制表符:实现自定义排序/分页/过滤/编辑行为的指南

javascript - 是否可以使用 JavaScript focus() 函数将焦点放在 <div> 上?

javascript - Cordova 应用程序从头开始,关于 ngCordova、Ionic 和 Typescript 的决定

javascript - 无法在 Firefox 上使用 date.parse。它适用于 Chrome

c# - 如何修改并保留 "dynamic"对象中的成员/嵌入数组(使用dynamic关键字声明的对象)

json - 如果没有第一个 DeserializationStrategy 参数,Kotlin 的 Json.decodeFromString 是如何工作的?