Lucky500
Lucky500

Reputation: 507

Iteratiing through localStorage object with jquery $.each

I am pretty close to getting this to work, but I could not find a response to this issue I am having. I am iterating over this object that I pulled from localStorage with the $.each method, but for some reason I keep getting the first value (image value) for all the 3 places where I have value. I am looking to get the title and the info as well. Any help would be great.

$('#btn-profile-view').on('click', function(){
  if(localStorage.getItem('itemsArray')){
    var savedLocal = localStorage.getItem('itemsArray');
    console.log('here is savedLocal: ', savedLocal);
    savedLocal = JSON.parse(savedLocal);
    var savedLocalMap = savedLocal.map(function(obj){
      var favsResult = $('.favs-display-data');
      $.each(obj, function(index, key, value){
        console.log('hey im value 0:', value);
        favsOutput = `<div class="col-lg-3 game">
                  <div class="view view-first">
                    <img src="${value}"/>
                    <div class="mask">
                      <h2>${value}</h2> 
                      <p>${value}</p>
                      <a href="#" class="info">♥</a>
                    </div>
                  </div>
                </div>`
        favsResult.append(favsOutput);
      });
    });
  }
});

Upvotes: 0

Views: 341

Answers (2)

neuhaus
neuhaus

Reputation: 4094

If you know the keys you should use them:

    favsOutput = `<div class="col-lg-3 game">
              <div class="view view-first">
                <img src="${obj['gameImg']}"/>
                <div class="mask">
                  <h2>${obj['gameTitle']}</h2> 
                  <p>${obj['gameInfo']}</p>
                  <a href="#" class="info">♥</a>
                </div>
              </div>
            </div>`

If you don't, when iterating over a hash the order is random.

Also each takes two arguments as said in the other answer.

Upvotes: 2

Mathias Mamsch
Mathias Mamsch

Reputation: 333

The jquery each callback takes as far as I can see only two parameters:

$.each(obj, function(key, value){
}

See here: http://api.jquery.com/jQuery.each/

Maybe this is already the problem?

Upvotes: 0

Related Questions