Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript push to specific key in array

I am working on Angular2 project using Firebase.

I need to push all query results under the same key in this.guestPush.

I have a multiple select element which contains different user levels. 4, 6, 9.

When one level is selected, the resulting object is saved in the guestPush array, but when another level is selected the following error occurs.

I get this error:

ERROR TypeError: this.guestPush[("filter" + i)].push is not a function

Here is my code :

  guestPush: any[] = new Array;

  level.forEach((lvl, index) => {

    this.db.object(`levelsUsers/${lvl}/users`)
      .subscribe(users => {

        if (this.guestPush['filter_' + i]) {
          this.guestPush['filter_' + i].push(users);
        } else {
          this.guestPush['filter_' + i] = users;
        }

      }, err => console.log(err));

  });

Edit ***

The users object which contains the user or users which match the filter:

Object {-KmSjAaxdCUvrPEQ8InI: Object, $key: "users", $exists: function}
  -KmSjAaxdCUvrPEQ8InI: Object
    admin:"false"
    description:"desc"
    ...
    ...
    verified:false
  __proto__:Object
  exists:function ()
  $key:"users"
  __proto__:Object

And this.guestPush[i] = users; creates an object like this:

[Object]
 0:Object
 -KmSjAaxdCUvrPEQ8InI: Object
    admin:"false"
    description:desc"
    ...
    ...
    verified:false
    __proto__:Object
  $exists:function ()
  $key:"users"
  __proto__:Object
 length:1
 __proto__:Array(0)

So in the next loop I want to add any new user objects next to -KmSjAaxdCUvrPEQ8InI or anything as long as it is added as a value of the 0 key.

like image 995
Ciprian Avatar asked Jul 14 '17 01:07

Ciprian


People also ask

How do you push an object with a key in an array?

To push an object into an array, call the push() method, passing it the object as a parameter. For example, arr. push({name: 'Tom'}) pushes the object into the array. The push method adds one or more elements to the end of the array.

How do you push a key and value in an array?

Just assign $array[$key] = $value; It is automatically a push and a declaration at the same time.

How do you push key-value?

Use map() to Push Key-Value Pair Into an Array in JavaScript The ECMAScript6 (ES6) introduces the arrow functions that let us write the function more concisely. We can only use this function if the function has only one statement.

How do you dynamically add an element to an array in TypeScript?

There are two ways to dynamically add an element to the end of a JavaScript array. You can use the Array. prototype. push() method, or you can leverage the array's “length” property to dynamically get the index of what would be the new element's position.


2 Answers

Looks like you are getting an array response and you are assigning it to the object :

this.guestPush['filter_' + i] = users;

so this.guestPush['filter_'+ i] is of type array now. If you want to add another users object to it , you are essentially adding another array to existing array. In that case shouldn't you be using concat ?

if (this.guestPush['filter_' + i]) {
    this.guestPush['filter_' + i].concat(users);
} else {
    this.guestPush['filter_' + i] = users;
}

if users is not an array, then it should be

if (this.guestPush['filter_' + i]) {
    this.guestPush['filter_' + i].push(users);
} else {
    this.guestPush['filter_' + i] = [users];
}
like image 109
Vamshi Avatar answered Sep 17 '22 08:09

Vamshi


I ended up using

 this.guestPush.push({ [i]: { [key]: obj } });
like image 30
Ciprian Avatar answered Sep 19 '22 08:09

Ciprian