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.
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.
Just assign $array[$key] = $value; It is automatically a push and a declaration at the same time.
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.
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.
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];
}
I ended up using
this.guestPush.push({ [i]: { [key]: obj } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With