[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? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
Hi, thanks for the love. 👍
You mean adding new values to an existing array, right? |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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'
})
export class ProductsService {
products: string[] = [];
constructor(
private storage: 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 (const product of products) {}
}); |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
You can do the |
Beta Was this translation helpful? Give feedback.
-
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... |
Beta Was this translation helpful? Give feedback.
-
From where does Your |
Beta Was this translation helpful? Give feedback.
.push()
may seem natural in JavaScript, but in fact it is quite unusual in programming languages.This lib is structured as a
Map
, and you can see on MDN, 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:
So you need something like that: