Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8168505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:32:59+00:00 2026-06-06T20:32:59+00:00

I want to loop true certain elements, in this case all with the class

  • 0

I want to loop true certain elements, in this case all with the class “widget”, and get there id and title(later on more stuff), store it as JSON in the localStorage(must be a string so we stringify the data), get the data from the localStorage and loop the results. All goes well until i want to loop it(JSON output is valid), this wont work, and i have been working on it for more than a day now so i hope that anybody see what i am doing wrong.

(yes i have looked on the web)

I am still a noob at this so if there are better ways to store and loop this let me know.

// html

<div class="widget" id="w1">
   <h2>some text</h2>
</div>

...
...
...

// setting the data

var storeStr = '';
    storeStr += '{"widget":[';
$('.widget').each(function(){                   
    storeStr += '{';                                
    storeStr += '"id": "'+$(this).attr('id')+'",';
    storeStr += '"title": "'+$(this).children('h2').text()+'"';
    storeStr += '},';
});
storeStr += ']}';
var storeStr = storeStr.replace(/\},]/g,'}]');// comma cleanup
localStorage.setItem('thelskey', storeStr);                     

// output(json valid)

{ 
  "widget":[
     {"id": "w1","title": "xxxxx"},
     {"id": "w2","title": "xxxxx"},
     {"id": "w3","title": "xxxxx"},
     {"id": "w4","title": "xxxxx"}
  ]
}

// the loop

var json = JSON.parse(localStorage.getItem('thelskey'));

for(var key in json){
    //output
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:33:01+00:00Added an answer on June 6, 2026 at 8:33 pm

    Your JSON object only has a single key, "widget". So there’s no point in looping through it. If you want to get all of the widget data, you need to loop through each item under the widget property, i.e.

    for (var key in json.widget) {
      // output
    }
    

    Otherwise, don’t wrap the array in the extra object, and just construct your JSON as:

    [
      {"id": "w1","title": "xxxxx"},
      {"id": "w2","title": "xxxxx"},
      {"id": "w3","title": "xxxxx"},
      {"id": "w4","title": "xxxxx"}
    ]
    

    I get the feeling you’re not understanding the structure of your JSON object, which is currently:

    {
      widget: [
        {
          id: "w1",
          title: "xxxxx"
        },
        {
          id: "w2",
          title: "xxxxx"
        },
        {
          id: "w3",
          title: "xxxxx"
        },
        ...
      ]
    }
    

    You can sort of think of it as a 3-dimensional associative array (or 3 layers of associative arrays nested within one another):

    /* in PHP code because JS doesn't have associative arrays */
    
    $json = array( // first layer
      'widget' => array( // second layer
        0 => array( // third layer
          'id' => 'w1',
          'tit;e' => 'xxxxx'
        ),
        1 => array( // third layer
          'id' => 'w2',
          'tit;e' => 'xxxxx'
        ),
        2 => array( // third layer
          'id' => 'w3',
          'tit;e' => 'xxxxx'
        ),
        ...
      )
    );
    

    You’re currently trying to loop through the first dimension of $json, which only has a single item. So the error you’re getting is because you’re trying to access:

    $json[$key]['widget']['id'] // $key = 'widget'
    

    when in fact you should be trying to access:

    $json['widget'][$key]['id'] // $key = 1, 2, 3, 4
    

    Or you can simply remove the first layer of your data structure and just access:

    $json[$key]['id'] // $key = 1, 2, 3, 4
    

    To avoid such problems in the future, make use of a JS console like Firebug’s or node.js’ or the built-in JS consoles that most modern browsers now have. Simply doing something like:

    console.log(json);
    

    or

    foreach (key in json) {
      console.log(key);
      console.log(json[key]);
    }
    

    would have revealed the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to loop through my Car class with reflection to remove all null
I have the code below and I want to loop this every 5 seconds.
In C, I want to loop through an array in this order for(int z
hi i want to loop through some tr's like this . <tr> <td> <input
I want to loop through all the variables stored in the session. I checked
Suppose we want to loop through all the items in a dropdown list and
Is there a way to hide top retweets from the twitter widget? This is
I want to loop through the controls on a UserControl and set a property
I want to loop through a database of documents and calculate a pairwise comparison
I want to loop over a series of files in a directory in batches

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.