Assign Empty Array to JSON object insted of null Assign Empty Array to JSON object insted of null json json

Assign Empty Array to JSON object insted of null


If you have a large number of null values that you want to change to empty array when using JSON#stringify, you can use a replace function (2nd param):

JSON.stringify(value[, replacer[, space]])

The function receives the key and the value, and you can decide, which value you want to return.

var data = {  var1: null,  var2: "someString",  var3: 1.3};var reslut = JSON.stringify(data, function(key, value) {  if (value === null) {    return [];  }  return value;});console.log(reslut);


To initialize an empty array in javascript simply write it like

var arr = [];

Example for JSON

var obj = {  arr: []};JSON.stringify(obj); // "{ "arr": [] }"


Initialize the object like this:

{  var1: [],  var2: "someString",  var3: 1.3,  ...}

Also,

curConfig.var1 = [];

should work, I don't know why it isn't working for you.