Javascript Data Type How to - Apply a function on each element of a json array








Question

We would like to know how to apply a function on each element of a json array.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var forEach=Function.prototype.call.bind([].forEach)
var jsonData = {"a":[1900,1910,1920,1930],"b":[12,19,8,55]}
<!-- w ww.  j a va 2s  .  com-->
function print(x,y){
    document.writeln(x+":"+y);
    document.writeln('<br/>');
}
forEach(jsonData.a, function(o,i){
    print(o,jsonData.b[i]);
})
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: