3

I'm new to javascript and firebase. I would just like to ask if there is a chance to call my variables outside of my function?

Here is my sample code:

function gotData (data) {
  console.log(data.val())
  var promotions = data.val()
  var keys = Object.keys(promotions)
  console.log(keys)
  for (var i = 0; i < keys.length; i++) {
    var k = keys[i]
    var name = promotions[k].promotionName
    var description = promotions[k].description
    var validityFrom = promotions[k].validityPeriodFrom
    var validityTo = promotions[k].validityPeriodTo
    var dateCreated = promotions[k].dateCreated
    var dateUpdated = promotions[k].dateUpdated
    console.log(name, description, validityFrom, validityTo, dateCreated, dateUpdated)
  }
}

Can I call name, description, validityFrom, etc outside of the function gotData? I'll appreciate a help. Thank you in advance! :)

6
  • 2
    "inside of a function outside of it"? Commented Sep 13, 2017 at 8:23
  • 1
    Short answer: no. Here is a page about "variable scope": w3schools.com/js/js_scope.asp Commented Sep 13, 2017 at 8:24
  • 2
    Can you show the code of call my variables inside of a function outside of it? Commented Sep 13, 2017 at 8:24
  • how about returning promotions[k] from your function. Commented Sep 13, 2017 at 8:26
  • Outside of my function :) Commented Sep 13, 2017 at 8:29

1 Answer 1

3

Define variable as global variable

//above function
var name, description, validityFrom, validityTo, dateCreated, dateUpdated;
function gotData (data) {
  console.log(data.val())
  var promotions = data.val()
  var keys = Object.keys(promotions)
  console.log(keys)
  for (var i = 0; i < keys.length; i++) {
    var k = keys[i]
    name = promotions[k].promotionName
    description = promotions[k].description
    validityFrom = promotions[k].validityPeriodFrom
    validityTo = promotions[k].validityPeriodTo
    dateCreated = promotions[k].dateCreated
    dateUpdated = promotions[k].dateUpdated
    console.log(name, description, validityFrom, validityTo, dateCreated, dateUpdated)
  }
}
// now you can access variables here
console.log(name, description, validityFrom, validityTo, dateCreated, dateUpdated)

EDIT

You can create an array to get all variables as you may have multidimensional data

var objData = [];
function gotData (data) {

  console.log(data.val())
  var promotions = data.val()
  var keys = Object.keys(promotions)

  for (var i = 0; i < keys.length; i++) {
    var tempObj = [];
    var k = keys[i]
    tempObj['promotionName'] = promotions[k].promotionName;
    tempObj['description'] = promotions[k].description;
    tempObj['validityPeriodFrom'] = promotions[k].validityPeriodFrom;
    tempObj['validityPeriodTo'] = promotions[k].validityPeriodTo;
    tempObj['dateCreated'] = promotions[k].dateCreated;
    tempObj['dateUpdated'] = promotions[k].dateUpdated;

    objData.push(tempObj);
  }
  console.log(objData)
}
Sign up to request clarification or add additional context in comments.

5 Comments

The values are assigned in a loop so they keep changing. This might not help much.
Well, which ones from the loop he wants to access then, can't have it both ways
An array should do the work. Not arrays of description, name, etc but multi dimensional array with indexes as description, name, etc. The array can be returned in the end of the function instead of global variables.
This still can be improved as now it is difficult to figure out which one is the name or description
added associative array code, but by doing this some array methods may not have accurate result :)

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.