Skip to content

[Feature Request] - New function > push: To add arrays to an existing object #407

Answered by cyrilletuzi
SvenBudak asked this question in Q&A
Discussion options

You must be logged in to vote

.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:

  1. get the current array (but note that usually you will/should already have this data somewhere in your code),
  2. push a new value into it,
  3. store the whole modified array in the Map.

So you need something like that:

@Injectable({
  providedIn: 'root'
})
export class ProductsService {

  products: string[] = [];

  constructor(
    private storage: StorageMap,
  ) {

    this.

Replies: 7 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by cyrilletuzi
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #407 on December 08, 2020 21:18.