console.log(shoppingCart.length)
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?
You wouldn't need the each to count the number of items, you can just:
itemCount = shoppingCart.length;
@REALRANDYALLEN - It gives me undefined
How did you push data in shoppingCart ?
@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.
@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 - It's really hard to say without seeing all the code in question
@pickab00 Could you post here the result of console.log(shoppingCart) ? A printscreen would be better.
@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
@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.
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?
@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 - It's really hard to say without seeing all the code in question
@VILFAGO - Can I post the whole file here? It would be really hard to give codes piece by piece
Give it a try
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.
@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
@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
@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.
For that you'd have to do a litte trick:
let objectKeys = Object.keys(shoppingCart)
objectKeys.forEach(key => {
console.log(shoppingCart[key])
})
Please or to participate in this conversation.