Replies: 4 comments
-
@ryansolid do you know any O(1) way to update and render an Object? |
Beta Was this translation helpful? Give feedback.
0 replies
-
With an array: O(n) + O(1) => O(n+1), with a map O(1) + O(1) => O(2)
Any ideas? |
Beta Was this translation helpful? Give feedback.
0 replies
-
@atk any insight? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I make a shopping website for which I build a cart. You can add products to the cart and remove them. Somewhere on the website it displays the current items in the cart, as well as the current total price.
I model the cart as a store which contains an object (not an array!), where the key is the product id and the value is the quantity.
(The reason I don't use an array is because then I can't update it efficiently. Incrementing or decrementing the amount of a product would require an O(n) search to find the item in the array, on the other hand with the store I can find it directly with they key.)
Now I want to write a component that displays the current cart. I would want to use the
For
component for that. However, what can I put in theeach=...
argument to iterate over the (key, value) pairs of my cart?I tried
Object.entries(...)
but it doesn't work. Then I thoughtunwrap()
might help but I also couldn't get it to work, and more importantly I think it would break reactivity or at least destroy the fine-grained approach.What is a working, idiomatic way to do this?
PS: I noticed that basically everywhere people use arrays to do such things and then iterate through the whole array to do changes. For example, this is an example of a piece of code from the Solid Docs:
What is going on? Comparing all the
id
s takes O(n) time. I thought Solid was about efficiency. This shouldn't be done. There must be a way to use some sort of map (i.e. object i.e. dictionary).Beta Was this translation helpful? Give feedback.
All reactions