-2

If there is an array in a function, and I would like to grab one value out of this array, depending if it exists or not, how would I do that with JavaScript?

To explain this question well look at the code below . . .

If we have a function:

function updateData(dataObj) {
    for (var i = 0; i < dataObj.length; i++) {
        var id = dataObj[i]['id'];
    }

    grabID(dataObj);
}

I am trying to grab whichever id that has a value, so I created the function grabID:

function grabID(dataObj) {
    for (var i=0; i< dataObj.length; i++) {
        var id = dataObj[i]['id'];
        if (typeof(id) == 'string') {
            //take that id and use it in the function below this one
        }
        else {
            continue;
        }       
    }      
}

Now this is the function that I want to place the id in, so I can draw a graph:

function drawGraph() {
    var id = //the id grabbed in the grabID function
             //use this id for drawing purposes
}

So the only help I need is how can I bring this id string from the grabID function. The parts that are commented are the parts that I need help with.

I hope this code helped explained what I am looking for exactly. I know I might have wrote unnecessary functions or lines of code, but this is the way I am thinking of in my head right now. The function updateData is not initially used to grab one id only (the id that has value). That is why I created another function called grabID.

talemyn
  • 7,822
  • 4
  • 31
  • 52
Mozein
  • 787
  • 5
  • 19
  • 33
  • Global variable? Or pass the ID from your `grabID` function to `drawGraph`/ – putvande Jan 26 '15 at 15:42
  • possible duplicate of [Best way to find an item in a JavaScript array?](http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array) – Blauharley Jan 26 '15 at 16:02

2 Answers2

0

Try calling drawGraph from within your grabID function, like so. Use the id variable as a parameter to drawGraph.

function grabID(dataObj) {
    for(var i=0; i< dataObj.length; i++){
        var id = dataObj[i]['id'];
        if(typeof(id) == 'string')
            drawGraph(id)
        else
            continue;
        }     
    }
}      

function drawGraph()
{
      var id = //the id grabbed in the grabID function
      //use this id for drawing purposes

}
Goodword
  • 1,565
  • 19
  • 27
0

You can call the grabID function and within it return the ID, then pass the result of the call to your drawGraph function:

function grabID(dataObj) {
          for(var i=0; i< dataObj.length; i++)
          {
              var id = dataObj[i]['id'];
              if(typeof(id) == 'string')
                   //take that id and use it in the function below this one
                   return id;
              else
                   continue;
          }
    // return null if no ID was retrieved
    return null;     
}

function updateData(dataObj) {
    for (var i = 0; i < dataObj.length; i++) {
       var id = dataObj[i]['id'];
    }
    var id = grabID(dataObj);
    if(id !== null) drawGraph(id);
}

function drawGraph(grabbedId)
{
    var id = grabbedId; //the id grabbed in the grabID function
    //use this id for drawing purposes
}
Neil Mountford
  • 1,951
  • 3
  • 21
  • 31