score:1

Accepted answer

yes this is definitely achievable!

try: child.val()[variable]

in javascript, we can access an object's properties by either using dots or brackets. for example, object.property or object["property"].

again, in your case, to access the firebase element with a variable, you could do child.val()[variable] where variable is some string or integer to access the firebase object's property. perhaps variable = 5 or variable = '5'.

score:0

unfortunately you're going to be struggling with how the firebase client handles arrays here.

the firebase client will interpret your data as an array and convert it to:

"mywords" : {
  "words" : [
    null,
    "tool box",
    "ice cream",
    "lavender",
    "speakers",
    "pepsi",
    "windows",
    "linux",
  ]
}

so it inserts an empty value in the first position of the array. if that is not a problem for your app, then you can accept this.

but for many apps this extra item is a problem. in those cases, we recommend that you prefix your numbers with some fixed string to ensure firebase doesn't does its string interpretation:

"mywords" : {
  "words" : {
    "item1" : "tool box",
    "item2" : "ice cream",
    "item3" : "lavender",
    "item4" : "speakers",
    "item5" : "pepsi",
    "item6" : "windows",
    "item7" : "linux",
  }
}

see this blog post for more information about how firebase handles arrays: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

score:1

you just need to replace child.val().today ---> child.val()[my variable]

var my_variable = moment().isoweekday(); //friday is 5
 var items = [];
  snap.foreach((child) => {

    items.push({
      title: child.val().title,
      profile:child.val().profile,
      test:child.val().test,
      special:child.val()[my_variable],
      _key: child.key,
    });
  });

Related Query

More Query from same tag