0

I have a project in ionic3 in which I have Firebase Array as follows.

List = {User3: true, User8: true, User9: false}

The parameter names are not fixed and can change. So when I receive the array in the above format, I need to list out only the users which are true. I need an array as follows.

User = {User3, User8,}

Fow do I list out the parameter names in a for loop.

for(let item of List){
//I need to write item.User3 in order to retrive the value. But parameter name User3 is not fixed.
}
0

1 Answer 1

1

Your List is not an "array". It's an object. It does not have "parameter names". It has properties. To get the properties (users) with a value of true, filter the object's properties, which you can obtain with Object.keys, for ones with a value of true. You could do this with something like

Object.keys(List).filter(user => List[user])

You cannot use for...of. That's for arrays, not objects.

You could also query just those nodes with a value of true, which can you find out how to do by consulting the Firebase documentation.

Minor point, but capitalizing variable names is confusing and non-standard. Normally capitalized identifiers are used for classes etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the solution.
Please consider upvoting the answers if they helped you @TapasMukherjee. Otherwise other SO users may seem that the answer is just a workaround and it's not actually a good way to solve the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.