0

I am new to localStorage. I set array in localstorage so how can get this value. My code as below.

$scope.lineItemID = data.id;
                    var itemtemp={
                        "itemid": data.id,
                        "qty": $scope.quantity
                    };
                    var itemqty=JSON.parse(localStorage.getItem("itemqty")) || [];
                    itemqty.push(itemtemp);
                    localStorage.setItem("itemqty", JSON.stringify(itemqty));

So my question is how can I get itemqty.qty as per itemid from localstorage

Harsh Gandhi
  • 59
  • 3
  • 11

3 Answers3

0

try Below code

    $.each(data.itemqty, function(index, item) {

// append data using html //use item.name

});

OR try below

$.each(data, function(idx, item){

// append data using html //

});

Som
  • 66
  • 8
0

You are quite simply creating an array of objects itemqty and saving it in the browser's storage. When you do this:

var itemqty=JSON.parse(localStorage.getItem("itemqty")) || [];
//itemqty is available to you as an array of objects.

Suppose you are looking for the associated quantity for some itemid stored in the variable foo. You just need to traverse the parsed itemqty like so:

 $.each(itemqty, function( index, value ) {
  if(value.itemid == foo)
     {
       console.log(value.qty);
       // value.qty is the required quantity
     }
});
Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
0
items=JSON.parse(localStorage.getItem('itemqty'));

    for (var i = 0; i < items.length; i++) {
        if(items[i].itemid === itmId) {
            return items[i].qty;
        }                       
    }

I am using it & it's working

Harsh Gandhi
  • 59
  • 3
  • 11