E
E
Evgeny Zhurov2018-01-31 17:28:40
JavaScript
Evgeny Zhurov, 2018-01-31 17:28:40

How to count the number of certain objects in LocalStorage?

Good afternoon, LocalStorage stores several objects that can be conditionally divided into two groups, the first is pairs with the comp-x key, where * - x are numeric indices, and the second is cart-x (with the same history). We need to get two numbers - the number of items with comp-x and the number of items with cart-x. How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-01-31
@Zhuroff

Why don't you write an array of one key at a time?
It would be much more convenient to work with an array:

var cartItems = JSON.parse(localStorage.getItem('cart'));
var itemsQuantity = cartItems.length;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question