[Solved] Iterate through an array in a map in javascript


Open your browsers javascript console (press F12) to see the output in the demo.

jsfiddle demo

var keys = ["key1","key2","key3"]

var data = {
"key1": {
    "target": "hostname",
    "datapoints": [
        [12, 1.42472334E9],
        [13, 1.424723355E9],
        [14, 1.42472337E9]]},
"key2": {
    "target": "hostname",
    "datapoints": [
        [15, 1.42472334E9],
        [16, 1.424723355E9],
        [17, 1.42472337E9]]}

}
//loop through the array named keys (mind that k is an index/integer)
for (var k in keys){
    //check if data has "key" as property (ie: key3 does not exist in data)
    if(data.hasOwnProperty(keys[k])){
        //loop through the array datapoints which is a property of the object named data 
        for ( var i =0; i< data[keys[k]]["datapoints"].length; i++){
          console.log(data[keys[k]]["datapoints"][i]); //outputs an array with 2 elements
        }
    }else{console.log(keys[k]+" does not exist in data")}

}

solved Iterate through an array in a map in javascript