如何在JavaScript中压缩两个数组? [英] How do I zip two arrays in JavaScript?

查看:121
本文介绍了如何在JavaScript中压缩两个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数组:

var a = [1, 2, 3]
var b = [a, b, c]

我想得到的结果是:

[[1, a], [2, b], [3, c]]

看起来很简单,但我无法理解。

It seems simple but I just can't figure out.

我希望结果是一个数组,两个数组中的每个元素都压缩在一起。

I want the result to be one array with each of the elements from the two arrays zipped together.

推荐答案

使用地图方法:

Use the map method:

var a = [1, 2, 3]
var b = ['a', 'b', 'c']

var c = a.map(function(e, i) {
  return [e, b[i]];
});

console.log(c)

DEMO

这篇关于如何在JavaScript中压缩两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆