也许现在在德国太晚了(凌晨 1:04)或者我错过了什么......
如何组合这两个数组:
a = [1, 2, 3] and b = [a, b, c] so that i get: [[1, a], [2, b], [3, c]]
谢谢...花更多的时间 sleep ! :)
编辑:
我看到这个函数叫做“zip”。 +1 知识
并且在 JavaScript 中没有内置函数用于此。 +1 知识
我应该说数组的长度是相等的。 -1知识
最佳答案
Array.prototype.zip = function(other) {
if (!Array.prototype.isPrototypeOf(other)) {
// Are you lenient?
// return [];
// or strict?
throw new TypeError('Expecting an array dummy!');
}
if (this.length !== other.length) {
// Do you care if some undefined values
// make it into the result?
throw new Error('Must be the same length!');
}
var r = [];
for (var i = 0, length = this.length; i < length; i++) {
r.push([this[i], other[i]]);
}
return r;
}
关于javascript - 在 Javascript 中组合数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5207665/