Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pickab00's avatar

How to count in Jquery each function

So here is my Jquery,

  $.each(shoppingCart, function(k, v) {
    
    console.log(v);

  
});

On each select item for the shopping cart, it adds a new object in to the shoppingCart. Now how can I count the number of objects?

0 likes
19 replies
realrandyallen's avatar

You wouldn't need the each to count the number of items, you can just:

itemCount = shoppingCart.length;
Vilfago's avatar

How did you push data in shoppingCart ?

pickab00's avatar

@REALRANDYALLEN - Here is what it returns with the OP question,

{shopItemId: 23, price: 11, img: "ivdtestupdate2.jpg", langInfo: {…}, thumbnail: "ivd_test_thumb.jpg", …}

{shopItemId: 78, price: 20, img: "Spring Rolls & Samosa 2.jpg", langInfo: {…}, thumbnail: "Spring Rolls & Samosa thumb 2.jpg", …}

{shopItemId: 86, price: 30, img: "Primavera.jpg", langInfo: {…}, thumbnail: "Primaveratn.jpg", …}

Also, Sometimes it does not update the shoppingcart when I choose an Item. But when I select some other item other than that, it updates. But if I do a raw output of shoppingCart it shows all the items in the shopping cart even though the item is not being added. Hope I am making sense. So what I did was I refreshed shoppingcart every second to see changes and that works fine (this was just for testing). But my question is, why does it not console log when i press the buy button but it is still inside the basket even if it is not console logging.

pickab00's avatar

@VILFAGO - I am using an onclick. Here,

   function buyItem() {
        var qty = $("#shopItem_qty_inputfield").val();
        if(qty == "" || qty == 0) {
        removeItem();
        return;
    }

    var item = shoppingCart[sel_shopItemId];
    item.qty = qty;

    $(sel_shopItem_elm).parent("li").find(".shopcart-icon").fadeIn();

    $("#shopItem_qty_container").slideDown().find("#shopItem_qty").text(item.qty);

    returnFromQtyInput();

}
pickab00's avatar

@JOSEFELIPETTO - https://ibb.co/8jwVL9v https://ibb.co/HDqC7bx

There are the images. Also why is there 2 objects?

I added 2 to basket. That is already displayed there. But why are there 2 objects. There are 2 shopItemId_23 with each console log. I am just console logging on click of add to basket button. So either way it is outputting the shoppingCart details.

Some do not update when added to basket. I mean I do not see the console log but when I open my basket, it is there

pickab00's avatar

@JOSEFELIPETTO - Referencing to the last answer. This is what I am getting as a result for the ones which are not console logging from the basket. When I console.log(shopingCart) it gives me this.

https://ibb.co/KXH5tbq

It has empty body? But inside is not empty. They are displayed in shoppingCart list function with no issues but when I do a each function, it does not work. Any ways, how can I take these kinds of empty ones and the previous ones and add all those and count the total?

pickab00's avatar

@JOSEFELIPETTO - For better understanding here is a video attached,

https://vimeo.com/311713711/f39ca51fa1

Please don't mind the console errors. As shown in the video, when I purchase 2 items it shows up as two. And when I add alcohol from the alcohol section, it is not added to that. But If I do a refresh and add just alcohol, it comes as blank object but the data is still inside it. I do not know what is going on. I can add other things (some of them atleast) to the shopping cart and have it inside of the shopItemId_23. But as shown, some other items from other categories and Alcohol does not add. They are blank objects if I add only them to shopping cart after a refresh.

pickab00's avatar

@VILFAGO - Can I post the whole file here? It would be really hard to give codes piece by piece

josefelipetto's avatar

The reason you cant use shoppingCart.length is because you're shoppingCart variable is an object and not an array. This is because when you do var item = shoppingCart[sel_shopItemId] and item.qty = qty, you're not pushing things to your variable, you're just updating it. For adding a new product you would have to do something like this:

shoppingCart.push({
    id: sel_shopItemId,
    price: somePrice,
    quantity: someQuantity. 
})

And for updating quantity, then you could do this:

shoppingCart.forEach(element => {
     if ( element.id === sel_shopItemId ) { 
        element.quantity = someQuantity
     }  
})

Then when it's all done, you can use the shoppingCart.length property.

pickab00's avatar

@VILFAGO - https://ufile.io/3bljx

That is the file. Don't mind some of the comments. It is a combo of node/spring so there are some deps. But that would not be required as that is not needed for jquery/js

pickab00's avatar

@JOSEFELIPETTO - Yes that I do get. But why am I getting an empty object when I select alcoholic? And how can I keep this as it is and just count the shopping cart items. Because when I push all to the shopping cart, it does show up on shopping cart but not when I console log

pickab00's avatar

@JOSEFELIPETTO - Ok it took me a while to understand what you are saying. So basically once i select something it creates an object. And it updates that object when I select other items. So how can I count this as it is is my question. I do not want to go through the coding and change it all over again.

josefelipetto's avatar

For that you'd have to do a litte trick:

let objectKeys = Object.keys(shoppingCart)

objectKeys.forEach(key => { 
      console.log(shoppingCart[key])
})

1 like

Please or to participate in this conversation.