javascript中是否有类似于Python的zip函数?也就是说,给定多个相等长度的数组,创建一个由对组成的数组。

例如,如果我有三个这样的数组:

var array1 = [1, 2, 3];
var array2 = ['a','b','c'];
var array3 = [4, 5, 6];

输出数组应该是:

var outputArray = [[1,'a',4], [2,'b',5], [3,'c',6]]

当前回答

不是Javascript本身内置的。一些常见的Javascript框架(如Prototype)提供了一个实现,或者你也可以自己编写。

其他回答

你可以使用ES6来创建实用函数。

控制台。json = obj => console.log(json .stringify(obj)); Const zip = (arr,…arrs) => 加勒比海盗。Map ((val, i) => arrs。Reduce ((a, arr) =>[…]A, arr[i]], [val])); / /实例 Const array1 = [1,2,3]; Const array2 = ['a','b','c']; Const array3 = [4,5,6]; 控制台。json (zip (array1 array2));/ /[[1, "一个"],[2,“b”],[3,“c”]] 控制台。Json (zip(array1, array2, array3));/ /[[1”“4],[2“b”5],[3“c”6]]

但是,在上述解决方案中,第一个数组的长度定义了输出数组的长度。

这里有一个解决方案,你可以更好地控制它。这有点复杂,但值得。

function _zip(func, args) { const iterators = args.map(arr => arr[Symbol.iterator]()); let iterateInstances = iterators.map((i) => i.next()); ret = [] while(iterateInstances[func](it => !it.done)) { ret.push(iterateInstances.map(it => it.value)); iterateInstances = iterators.map((i) => i.next()); } return ret; } const array1 = [1, 2, 3]; const array2 = ['a','b','c']; const array3 = [4, 5, 6]; const zipShort = (...args) => _zip('every', args); const zipLong = (...args) => _zip('some', args); console.log(zipShort(array1, array2, array3)) // [[1, 'a', 4], [2, 'b', 5], [3, 'c', 6]] console.log(zipLong([1,2,3], [4,5,6, 7])) // [ // [ 1, 4 ], // [ 2, 5 ], // [ 3, 6 ], // [ undefined, 7 ]]

除了ninjagecko出色而全面的回答外,将两个js数组压缩成“元组模拟”所需要的是:

//Arrays: aIn, aOut
Array.prototype.map.call( aIn, function(e,i){return [e, aOut[i]];})

Explanation: Since Javascript doesn't have a tuples type, functions for tuples, lists and sets wasn't a high priority in the language specification. Otherwise, similar behavior is accessible in a straightforward manner via Array map in JS >1.6. (map is actually often implemented by JS engine makers in many >JS 1.4 engines, despite not specified). The major difference to Python's zip, izip,... results from map's functional style, since map requires a function-argument. Additionally it is a function of the Array-instance. One may use Array.prototype.map instead, if an extra declaration for the input is an issue.

例子:

_tarrin = [0..constructor, function(){}, false, undefined, '', 100, 123.324,
         2343243243242343242354365476453654625345345, 'sdf23423dsfsdf',
         'sdf2324.234dfs','234,234fsf','100,100','100.100']
_parseInt = function(i){return parseInt(i);}
_tarrout = _tarrin.map(_parseInt)
_tarrin.map(function(e,i,a){return [e, _tarrout[i]]})

结果:

//'('+_tarrin.map(function(e,i,a){return [e, _tarrout[i]]}).join('),\n(')+')'
>>
(function Number() { [native code] },NaN),
(function (){},NaN),
(false,NaN),
(,NaN),
(,NaN),
(100,100),
(123.324,123),
(2.3432432432423434e+42,2),
(sdf23423dsfsdf,NaN),
(sdf2324.234dfs,NaN),
(234,234fsf,234),
(100,100,100),
(100.100,100)

相关的性能:

使用map over for loops:

请参阅:将[1,2]和[7,8]合并为[[1,7],[2,8]的最有效方法是什么

注意:基本类型如false和undefined不具有原型对象层次结构,因此不公开toString函数。因此,这些在输出中显示为空。 由于parseInt的第二个参数是基数/数字基数,要将数字转换为基数/数字基数,并且由于map将索引作为第二个参数传递给它的参数函数,因此使用包装器函数。

这是我的解决方案

let zip = (a, b) => (a.length < b.length
  ? a.map((e, i) => [e, b[i]])
  : b.map((e, i) => [a[i], e]))

python zip函数的生成器方法。

function* zip(...arrs){
  for(let i = 0; i < arrs[0].length; i++){
    a = arrs.map(e=>e[i])
    if(a.indexOf(undefined) == -1 ){yield a }else{return undefined;}
  }
}
// use as multiple iterators
for( let [a,b,c] of zip([1, 2, 3, 4], ['a', 'b', 'c', 'd'], ['hi', 'hello', 'howdy', 'how are you']) )
  console.log(a,b,c)

// creating new array with the combined arrays
let outputArr = []
for( let arr of zip([1, 2, 3, 4], ['a', 'b', 'c', 'd'], ['hi', 'hello', 'howdy', 'how are you']) )
  outputArr.push(arr)

没有等价的函数。如果你只有几个数组,你应该使用for循环获取一个索引,然后使用索引访问数组:

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

for (let i = 0; i < Math.min(array1.length, array2.length); i++) {
    doStuff(array1[i], array2[i]);
}

如果你有更多的数组,你可以在数组上有一个内循环。