简体   繁体   中英

how to parse integer array to json

this code convert integer array to json. The objective is helper insert into on database.

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

const jsonResult = `{${list}}`

output

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

x = JSON.stringify(list);

x =>> list JSON STRING

your list variable is not json, you have to change the data types of every single index, your solution is like below:

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

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

console.log(sample())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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