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中是否有类似于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]]
当前回答
下面是一个快速有效的方法,使用iter-ops库,operator zip:
const {pipe, zip} = require('iter-ops');
const i = pipe(array1, zip(array2, array3));
console.log(...i); //=> [ 1, 'a', 4 ] [ 2, 'b', 5 ] [ 3, 'c', 6 ]
标准库将所有输入作为可迭代对象处理,因此它们只迭代一次。它可以以同样的方式处理所有类型的可迭代对象——iterable, AsyncIterable, Iterator, AsyncIterator。
附注:我是iter-ops的作者。
其他回答
我在纯JS中尝试了一下,想知道上面发布的插件是如何完成这项工作的。这是我的结果。首先我要说的是,我不知道这在IE和类似的软件中会有多稳定。这只是一个快速的模型。
init(); function init() { var one = [0, 1, 2, 3]; var two = [4, 5, 6, 7]; var three = [8, 9, 10, 11, 12]; var four = zip(one, two, one); //returns array //four = zip(one, two, three); //returns false since three.length !== two.length console.log(four); } function zip() { for (var i = 0; i < arguments.length; i++) { if (!arguments[i].length || !arguments.toString()) { return false; } if (i >= 1) { if (arguments[i].length !== arguments[i - 1].length) { return false; } } } var zipped = []; for (var j = 0; j < arguments[0].length; j++) { var toBeZipped = []; for (var k = 0; k < arguments.length; k++) { toBeZipped.push(arguments[k][j]); } zipped.push(toBeZipped); } return zipped; }
虽然不是无懈可击,但还是很有趣。
和@Brandon一样,我推荐Underscore的zip功能。但是,它的作用类似zip_longest,根据需要附加未定义的值以返回最长输入的长度。
我使用mixin方法用zipshort扩展下划线,它的作用类似于Python的zip,基于库自己的zip源代码。
你可以在你的普通JS代码中添加以下代码,然后像调用下划线一样调用它:_。zipShortest ([1, 2, 3], [a])返回[[1,' ']],例如。
// Underscore library addition - zip like python does, dominated by the shortest list
// The default injects undefineds to match the length of the longest list.
_.mixin({
zipShortest : function() {
var args = Array.Prototype.slice.call(arguments);
var length = _.min(_.pluck(args, 'length')); // changed max to min
var results = new Array(length);
for (var i = 0; i < length; i++) {
results[i] = _.pluck(args, "" + i);
}
return results;
}});
Mochikit库提供了这个函数和许多其他类似python的函数。Mochikit的开发者也是一个Python爱好者,所以它具有Python的一般风格,并且还将异步调用包装在一个扭曲的框架中。
除了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将索引作为第二个参数传递给它的参数函数,因此使用包装器函数。
带有生成器的现代ES6示例:
function *zip (...iterables){
let iterators = iterables.map(i => i[Symbol.iterator]() )
while (true) {
let results = iterators.map(iter => iter.next() )
if (results.some(res => res.done) ) return
else yield results.map(res => res.value )
}
}
首先,我们得到一个iterables列表作为迭代器。这通常是透明地发生的,但在这里我们明确地进行,因为我们逐步让步,直到其中一个耗尽。我们检查给定数组中的任何结果(使用.some()方法)是否已耗尽,如果是,则中断while循环。