by @kodeazy
why JSON.stringify returning [] for array in javascript
Given an Array
with some string characters as key but if I print the array showing []
as below
var array = [];
array['name'] = 'James';
array['age'] =25;
console.log(JSON.stringify(array));
output
[]
Arrays
does not use string as element index, it can use only Integers
as Index
If we want to use String
as element Index
, we can use Object
instead of Array
Object
accepts String
as key
To initialize Object
we use var array1={};
var array1 = {};
array1['name'] = 'James';
array1['age'] = 25;
console.log(JSON.stringify(array1));
output
{"name":"James","age":25}