Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork68
[Feature Request] - New function > push: To add arrays to an existing object#407
-
Hi! First of all I want to thank you for sharing this great library with us! :) I always had problems with pushing more arrays into an existing object. Are you planning to implement a new function for this? |
BetaWas this translation helpful?Give feedback.
All reactions
.push() may seem natural in JavaScript, but in fact it is quite unusual in programming languages.
This lib is structured as aMap, and you can see onMDN, there is no.push() method in such data structures. When you want to modify a value, you need to set the whole new value.
So in the case of an array, there are 3 steps:
- get the current array (but note that usually you will/should already have this data somewhere in your code),
- push a new value into it,
- store the whole modified array in the Map.
So you need something like that:
@Injectable({providedIn:'root'})exportclassProductsService{products:string[]=[];constructor(privatestorage:StorageMap,){this.…
Replies: 7 comments
-
Hi, thanks for the love. 👍
You mean adding newvalues to an existingarray, right? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Hmm. I always thought that was called an object.) Unfortunately I am not a good coder. I am actually a frontend designer :) Since a few hours I'm trying to add more arrays I mean, I have something like this in storage at first: And I'd like to turn it into this: Up to four items. I also do not understand why I cannot iterate through vehicles: I know that this is an issue board and not a stackoverflow ;) But is what I have in mind easily possible only with your service? |
BetaWas this translation helpful?Give feedback.
All reactions
-
This lib is structured as a So in the case of an array, there are 3 steps:
So you need something like that: @Injectable({providedIn:'root'})exportclassProductsService{products:string[]=[];constructor(privatestorage:StorageMap,){this.storage.get('products',{type:'array',items:{type:'string',},}).pipe(/* Following operators are optional, they are here to be sure to have a default value */catchError(()=>of([])),map((products)=>products??[]),).subscribe((products)=>{this.products=products;});}add(product:string):void{this.products.push(product);this.storage.set('products',this.products).subscribe();}} For your second question, If your value is an array, just iterate normally over it: this.storage.get('products',{type:'array',items:{type:'string',},}).pipe(/* Following operators are optional, they are here to be sure to have a default value */catchError(()=>of([])),map((products)=>products??[]),).subscribe((products)=>{for(constproductofproducts){}}); |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Oh that helped me alot! Thank you! But i really need the watch function ;) And isnt it possible to use an angular interface for the schema? because everytime i change some on the data structure i also need to change it in the schemas to. |
BetaWas this translation helpful?Give feedback.
All reactions
-
You can do the |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Thank you! I have tried many things, but I guess I just don't have enough basic knowledge in JavaScript. 😅 HTML This results in endless loop for me... |
BetaWas this translation helpful?Give feedback.
All reactions
-
From where does Your |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #407 on December 08, 2020 21:18.