Answer the question
In order to leave comments, you need to log in
How to make a basket object in js?
The cart is stored in localStorage, and therefore in the form of json. What structure will be more correct to operate conveniently with data.
At the beginning there was an idea:
product_1: {count:1, title: 'title'},
product_2: {count:1, title: 'title'},
articul_1: {count:1, title: 'title'},
JSON.stringify(cart); //дает пустой массив
0: {item: 'product_1', count:1, title: 'title'},
1: {item: 'product_2', count:1, title: 'title'},
2: {item: 'atricul_1', count:1, title: 'title'},
Answer the question
In order to leave comments, you need to log in
You're right, overkill to help. In general, in terms of the client side, enumerations are not so terrible, the browser launches a bunch of threads for its own calculations and does any enumeration quickly enough. It's another matter to save and pull out all this stuff from local storage. It will take more time than searching for values.
Look towards the Immutable.js library .
Retrieving, adding, and checking for the presence of data in an immutable object is pure pleasure.
PS: there is conversion to JSON. The library documentation is excellent.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question