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
.