Javascript Data Type How to - Map two array into one array








Question

We would like to know how to map two array into one array.

Answer


<!--from w ww  .  j  a  va  2s .  c  om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>

function zip(a, b) {
    return a.map(function(x, i) {
        return [x, b[i]];
    });
}
x = [1,2,3,4,5,6,7,8,9];
y = [5,4,8,5,5,5,5,5,5];
z = zip(x, y)
document.writeln(JSON.stringify(z))

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: