Home > OS >  How to remove quotes from json null when concatenating into a string?
How to remove quotes from json null when concatenating into a string?

Time:01-27

I can't get rid of the single quote from around null here:

const obj = {"ItemData": {
        "ItemId": 4335549,
        "CustID": null,
        "dateCreated": "2021-02-01 07:50:51.1670000",
        "amount": 10.99         
    }
}

let x= "'"   obj.ItemData.ItemId   "|''"   obj.ItemData.CustID   "''|"   obj.ItemData.dateCreated   
"|"   obj.ItemData.amount   "'"
regx = /'null'/ig
console.log(x.replaceAll(regx, null))

I get this:

'4335549|'null'|2021-02-01 07:50:51.1670000|10.99'

I want this:

'4335549|null|2021-02-01 07:50:51.1670000|10.99'

Also tried standard replace, same result.

How to remove the quotes from around null?

CodePudding user response:

try this

regx = /''null''/ig
console.log(x.replaceAll(regx, null));

CodePudding user response:

You can use the following code to only insert the quotes when the value is not null

let x= "'"   obj.ItemData.ItemId   "|"  
    ((obj.ItemData.CustID === null) ? "null" : ("'"   obj.ItemData.CustID   "'"))   "|"   obj.ItemData.dateCreated   "|"   obj.ItemData.amount   "'"
  •  Tags:  
  • Related