I am new to JavaScript and I have been trying to run the following code with an expected result like this:
[["00:04:12","05:54:46"],["06:06:42","12:45:22"],["12:51:11","15:56:11"]]
However, my script is not working as expected. Can someone please help me identify what's wrong with my code? Thank you
<!DOCTYPE html>
<html>
<body>
<p>Access a JSON object :</p>
<p id="demo"></p>
<script>
var myObj;
var m1 = [];
myObj = [{
"machine": "M-MBH-(2)",
"time": [{
"start": "06:24:23",
"end": "16:45:37"
}]
}, {
"machine": "M-MD2.5",
"time": [{
"start": "00:04:12",
"end": "05:54:46"
}, {
"start": "06:06:42",
"end": "12:45:22"
}, {
"start": "12:51:11",
"end": "15:56:11"
}]
}];
for (var i in myObj) {
var obj = myObj[i].time;
if (obj === "M-MD2.5") {
obj.forEach(function(time) {
var pair = [];
pair.push(time.start);
pair.push(time.end);
m1.push(pair);
});
}
}
document.getElementById("demo").innerHTML = JSON.stringify(m1);
</script>
</body>
</html>