0

I have a cart array with items that I'm counting like this:

cart.length

The problem is, if one item has 6 quantity and the other has 1 quantity, it shows only 2 instead of 7. How do I count the total of the items? The structure is like this:

enter image description here

I've tried a million things and none of them works. The closest I think I got is:

var total = 0;
        $.each(cart, function(index, value) { 
            var myqty = parseInt(qty, 1);
            sum += myqty;
        });

console.log(total);

But that also doesn't work. Any hints are greatly appreciated. The error I get is:

Uncaught ReferenceError: qty is not defined
user1996496
  • 972
  • 2
  • 10
  • 24
  • But he did not get answer with his previous post. – Himanshu Upadhyay Sep 26 '17 at 10:14
  • @HimanshuUpadhyay: He would've if he read the dupe targets. – Cerbrus Sep 26 '17 at 10:15
  • I read the previous answers ...I didn't find a solution to my problem. I was asked to explain further what I tried and that's what I did. – user1996496 Sep 26 '17 at 10:17
  • @user1996496: Then you update your question. You don't delete and re-post it. – Cerbrus Sep 26 '17 at 10:18
  • @Cerbrus I could not. I tried. That's why I deleted it. The edit option was gone. And now you're attacking my post here too. Why? – user1996496 Sep 26 '17 at 10:19
  • _"The edit option was gone."_? I doubt that. I'm not "attacking" your post. It's a duplicate, so I closed it. – Cerbrus Sep 26 '17 at 10:20
  • @Cerbrus It was gone. Otherwise I would have edited it. I'm not stupid, I'm desperate for help. I've been at this for 10 hours straight before asking for help and now I'm being maliciously attacked no matter what I do. I was just asking for help. I posted all that I've tried. I have no options left. Why be mean to people instead of helping them? – user1996496 Sep 26 '17 at 10:24
  • @Cerbrus Now what do you suggest? You told everyone to delete their answers here and my other post is gone. What do I do now? Tell me. – user1996496 Sep 26 '17 at 10:25
  • @user1996496: You're aware that I can see that _you_ deleted the previous question yourself, right? I never _told_ anyone to delete any answers. I'd suggest having a good look at the dupe targets again. Try to understand what `reduce` does, and how it may work for you. – Cerbrus Sep 26 '17 at 10:25
  • @Cerbrus I deleted the question because I couldn't edit it. – user1996496 Sep 26 '17 at 10:40
  • 1
    @user1996496 Can you take a screen shot of the previous question to show that the "edit" option was missing. I find it hard to believe that that was the case. In the meantime edit *this* question to explain why the answers in the linked duplicates don't help you. – ChrisF Sep 26 '17 at 13:19

0 Answers0