简体   繁体   English

如何将 integer 数组解析为 json

[英]how to parse integer array to json

this code convert integer array to json.此代码将 integer 数组转换为 json。 The objective is helper insert into on database.目标是帮助插入数据库。

const list = [
'101',  '1922', '2205',
'2495', '2568']

const jsonResult = `{${list}}`

output output

'{101,1922,2205,2495,2568}'

x = JSON.stringify(list); x = JSON.stringify(list);

x =>> list JSON STRING x =>> 列表 JSON 字符串

your list variable is not json, you have to change the data types of every single index, your solution is like below:您的列表变量不是 json,您必须更改每个索引的数据类型,您的解决方案如下:

const list = [
    '101', '1922', '2205',
    '2495', '2568']

const changeToNumber = () => {
    let result = []
    list.forEach(item => {
        result = [...result, parseInt(item)]
    })
    return result
}

console.log(sample())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM