-6

I have an array which is declared like this:

array = [];

and has values as shown below -

....
ChIJOaegwbTHwoARg7zN_9nq5Uc:"ChIJOaegwbTHwoARg7zN_9nq5Uc"
ChIJXTwCdefHwoAR9Jr4-le12q4:"ChIJXTwCdefHwoAR9Jr4-le12q4"
....

These values are printed when I do console.log(array);

When I try to do a JSON.stringify(array), it does not seem to work. I want to store this array in localStorage using localStorage.setItem().

I tried an example like in this in the browser console:

var arr=[]
arr[0] = 1
arr[1] = 2
JSON.stringify(arr);

And the above example worked perfectly fine.

Please provide your inputs, I have been stuck at this for hours.

john
  • 45
  • 1
  • 6
  • Please add json for which you are getting error – Alpesh Jikadra Apr 06 '18 at 06:50
  • 6
    Step 1: Learn the difference between arrays and objects. There is no such thing as an associative array in JavaScript. You added arbitrary custom properties to an array, but those are not _elements_ of the array, so they don’t get serialized by JSON.stringify. – CBroe Apr 06 '18 at 06:51
  • Both Objects and Arrays are associative, only that they keys are permitted to only be or be convertible to Strings and Numbers respectively. – Dan D. Apr 06 '18 at 06:54
  • Possible duplicate of [JavaScript associative array to JSON](https://stackoverflow.com/questions/4425289/javascript-associative-array-to-json) – Andreas Apr 06 '18 at 07:00

4 Answers4

1

You are trying to assign values in array like objects ; index can only be o,1,2 etc. not the strings like you have used. If you must do this, create an array of objects

1

the problem is that you are trying to set the array index as strings 'ChIJOaegwbTHwoARg7zN_9nq5Uc' and 'ChIJXTwCdefHwoAR9Jr4-le12q4' and although the browser seems to print it in the console, but array considers only integer keys as valid indices, so if you try to print array.length, it will print 0 and hence operations such as JSON.stringify(array) don't return you anything

var array = [];
array['ChIJOaegwbTHwoARg7zN_9nq5Uc'] = "ChIJOaegwbTHwoARg7zN_9nq5Uc";
array['ChIJXTwCdefHwoAR9Jr4-le12q4'] = "ChIJXTwCdefHwoAR9Jr4-le12q4";
console.log(array);
console.log(array.length)

What you need is not an array but an object

var obj = {};
obj['ChIJOaegwbTHwoARg7zN_9nq5Uc'] = "ChIJOaegwbTHwoARg7zN_9nq5Uc";
obj['ChIJXTwCdefHwoAR9Jr4-le12q4'] = "ChIJXTwCdefHwoAR9Jr4-le12q4";
console.log(obj);
console.log(JSON.stringify(obj))
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

That's not an array, but an object. Objects don't stringify as well as arrays do.

Basically:

String = 'This is a string'

Array = [
  'This is a string',
  'And so is this'
]

Object = {
  firstString  : 'This is a string',
  secondString : 'And so is this.'
}
Breki Tomasson
  • 207
  • 2
  • 11
0

This works for me in a plunker.

let array = [];
array.push({"ChIJOaegwbTHwoARg7zN_9nq5Uc":"ChIJOaegwbTHwoARg7zN_9nq5Uc"});
array.push({"ChIJXTwCdefHwoAR9Jr4-le12q4":"ChIJXTwCdefHwoAR9Jr4-le12q4"});
console.log(JSON.stringify(array));

If you do a console.log(array), you should see something like this: enter image description here

ForestG
  • 17,538
  • 14
  • 52
  • 86