javascript JSON.parse result problem on empty array with localStorage - Stack Overflow

I initialized a localStorage variable with the following function:if (!localStorage["store"])

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.

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.

Share Improve this question asked Mar 24, 2011 at 18:41 Carles AndresCarles Andres 1,8111 gold badge16 silver badges22 bronze badges 6
  • Possible repost stackoverflow./questions/5173441/javascript-push-to-array – Lance Commented Mar 24, 2011 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 Commented Mar 24, 2011 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 Commented Mar 24, 2011 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 Commented Mar 24, 2011 at 19:06
  • console.log(localStorage["store"]) ouput is "[]" – Carles Andres Commented Mar 24, 2011 at 19:33
 |  Show 1 more ment

2 Answers 2

Reset to default 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.

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385613a4625425.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信