Home > Software design >  how to parse integer array to json
how to parse integer array to json

Time:11-19

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}'

CodePudding user response:

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(changeToNumber())

CodePudding user response:

x = JSON.stringify(list);

x =>> list JSON STRING

  • Related