3

I initialized a localStorage variable with the following function:

    if (!localStorage["store"]) {
       var aaa = new Array();
       localStorage["store"] = JSON.stringify(aaa);
    }   

It seems to work ok, but when I try use that array in order to add elements with the following code:

  var one_stat = new Array( s1, s2 , resultat );
  var statistics = JSON.parse(localStorage["store"]);
  statistics.push( one_stat );
  localStorage["store"] = JSON.stringify(statistics);

I get the following error:

Uncaught TypeError: Object [] has no method 'push'

I am using Google Chrome 10.0.648.151 on Ubuntu.

Does anyone know what I might be doing wrong?

Thanks.

Carles Andres
  • 1,761
  • 1
  • 15
  • 22
  • Possible repost http://stackoverflow.com/questions/5173441/javascript-push-to-array – Lance Mar 24 '11 at 19:00
  • console.log(localStorage.store); My guess is that you initialized the localStorage.store to someting other than an array and now that it's set your code isn't resetting it to a blank array since you if statement would be evaluating false – JaredMcAteer Mar 24 '11 at 19:01
  • What's the output of `alert(localStorage["store"])` ? Also, if you just are having trouble with an empty array, did you try simply `localStorage["store"] = "[]"` ? – Aleadam Mar 24 '11 at 19:03
  • It doesn't matter what he has in localStorage array... He's attempting to place the text into an object and use the array push method to add additional data to his JSON object. Object's do not have a push method. It will fail, period. – Lance Mar 24 '11 at 19:06
  • `console.log(localStorage["store"])` ouput is `"[]"` – Carles Andres Mar 24 '11 at 19:33
  • Of course it is... You haven't stored anything in localStorage["store"]. – Lance Mar 24 '11 at 20:46

2 Answers2

2

I tried the following code and it worked as expected:

<!DOCTYPE html>         
<html>                  
<head>
<title>Prova</title>    
</head>                 
<body>
<script type="text/javascript"> 
        if (!localStorage["stor"] ) {

                localStorage["stor"] = JSON.stringify([]);
        }               

        var aa = JSON.parse( localStorage["stor"] ) ;
        console.log( aa ) ;
        aa.push( [ 1 , 2, 2 ] ) ;
        localStorage["stor"] = JSON.stringify( aa ) ;
</script>
I am trying, man
</body>
</html> 

It seems it has something to do with Prototype library, which I am using. Have a look at this: JSON.stringify() array bizarreness with Prototype.js

I still haven't worked out a solution, but I believe I on the right path.

Community
  • 1
  • 1
Carles Andres
  • 1,761
  • 1
  • 15
  • 22
  • Solved it. It was a known conflict with Prototype 1.6.x . I updated to Prototype 1.7 and ii tot solved. More information here: [https://prototype.lighthouseapp.com/projects/8886/tickets/730-prototypejs-breaks-firefox-35-native-json] . Thanks @Lance @Aleadam and @OriginalSyn for your help – Carles Andres Mar 26 '11 at 15:43
0

Essentially, a JSON object is not an array and the push method only works with arrays in javascript. Is there a reason why you're not using the object literal method to add the array data to your JSON object?

e.g.

statistics.variable = one_stat;
Lance
  • 1,889
  • 14
  • 12
  • 1
    I am sorry I don't understand. Can't I save anything (e.g. an array) directly to localStorage["store"]? I mean, isn't JSON.parse supposed to return my empty array? – Carles Andres Mar 24 '11 at 19:15
  • JavaScript arrays and objects have different methods. The one method you're using--'push'--does not work with your JSON object, because objects don't have a push method. Arrays, by contrast, do have a push method. JSON.parse returns a JSON object, _not_ an array. http://www.json.org/js.html more information can be found there. What exactly are you trying to accomplish? It looks like you want to save your statistical data to an array in localStorage['store']. Why are you using JSON.parse at all if all you want is an array storage container? – Lance Mar 24 '11 at 20:00
  • Lance. I appreciate all your answers but they don't solve my problem. I need to know how can I store an array into localStorage which I can later use 'push' on. I know I could use objects but I am sure there must be a way I can store an empty array or a whole n-dimensional array into localStorage and retrieve it back as it previously was. I would appreciate any help. – Carles Andres Mar 24 '11 at 22:08