Reputation: 3226
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.
Upvotes: 14
Views: 13200
Reputation: 8914
Here's your problem,
guestPush: any[] = new Array;
'filter_' + i // this will yield a string.
this.guestPush['filter_' + i])
I could define an object:
let name = {};
name['x'] = 'blah';
or name.x = 'blahblah'; // name.x does same thing as name['x']
I think your thinking name['x'] = 'blah';
is an array, when it's object syntax.
Consequently you need to think about implementing a Dictionary.
Upvotes: 2
Reputation: 38713
@Ciprian : As per your answer.
You need a small change from your code to use. I hope this below code enough to you.
let key ='filter_';
this.guestPush.push({ key + i : users });
Upvotes: 2
Reputation: 9351
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];
}
Upvotes: 11
Reputation: 276343
Typescript push to specific key in array
You need to use the splice
method on the array : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Upvotes: 1