简体   繁体   English

如何将数组与对象和键组合成多维数组

[英]How to combine array to array multidimensional with object and key

How to combine 2 array to be 1 array with object and key. 如何将2个数组与对象和键合并为1个数组。 array1 = [a, b, c , d] array2 = [z, y, x, w] I want to be an array like result = [[foo: a, bar: z], [foo: b, bar: y], [foo: c, bar: x], [foo: d, bar: w]] . array1 = [a, b, c , d] array2 = [z, y, x, w]我想成为一个数组,例如result = [[foo: a, bar: z], [foo: b, bar: y], [foo: c, bar: x], [foo: d, bar: w]] I just can combine without object and key like this: 我可以像这样没有对象和键地组合:

var array1 = [a, b, c , d];
var array2 = [z, y, x, w];
var result = [];
result = $.map(array1, function (el, idx) {
  return [[el, array2[idx]]];
});
output: [[a, z],[b, y],[c, x],[d, w]];

If you want an array of objects with keys foo and bar (which it more or less looks like you do), you are almost there. 如果您想要一个带有键foobar的对象数组(看起来或多或少像您一样),那么您就差不多到了。 You just need to make an object with map() rather than an array: 您只需要使用map()而不是数组来创建对象:

 var array1 = ['a', 'b', 'c' , 'd']; var array2 = ['z', 'y', 'x', 'w']; let result = array1.map((item, index) => ({foo:item, bar: array2[index]})) console.log(result) 

Clean-up 清理

First of all, a little clean-up of your initial version: 首先,对初始版本进行一些清理:

 var array1 = ['a', 'b', 'c', 'd']; var array2 = ['z', 'y', 'x', 'w']; var result = array1.map(function (el, idx) { return [[el, array2[idx]]]; }); console.log(result) 
 .as-console-wrapper {height: 100vh !important;} 

Note that the array values here are listed as strings, just to show what's happening. 请注意,此处的数组值被列为字符串,只是为了显示正在发生的事情。 But also note that we can use the map method of Arrays rather than jQuery's version. 但也请注意,我们可以使用Arrays的map方法而不是jQuery的版本。

Changing to objects 更改为对象

But now we can easily change this to get the output you are looking for: 但是现在我们可以轻松地更改它,以获取您想要的输出:

 var array1 = ['a', 'b', 'c', 'd']; var array2 = ['z', 'y', 'x', 'w']; var result = array1.map(function (el, idx) { return {foo: el, bar: array2[idx]}; }); console.log(result) 
 .as-console-wrapper {height: 100vh !important;} 

More general 更一般

The operation of pair-wise combining two lists is often called zip -- think of it like a zipper on the two lists. 成对组合两个列表的操作通常称为zip将其视为两个列表上的拉链。

We can use something much like your code to write a naive zip function: 我们可以使用类似于您的代码的方式来编写朴素的zip函数:

 const zip = function(xs, ys) { return xs.map(function(x, i) {return [x, ys[i]]}) } const array1 = ['a', 'b', 'c' , 'd']; const array2 = ['z', 'y', 'x', 'w']; const result = zip(array1, array2) console.log(result) 
 .as-console-wrapper {height: 100vh !important;} 

A general function for your problem 您问题的一般功能

Combining the abstraction from this version with the expansion we used in creating objects, we could write a function zipWith that accepts the two lists as well as a function used to combine an element from each into a new value: 将这个版本的抽象与用于创建对象的扩展相结合,我们可以编写一个函数zipWith ,该函数接受两个列表以及一个用于将每个元素组合成一个新值的函数:

 const zipWith = function(fn) { return function(xs, ys) { return xs.map(function(x, i) {return fn(x, ys[i]);}) } } const array1 = ['a', 'b', 'c' , 'd']; const array2 = ['z', 'y', 'x', 'w']; const foobar = (x, y) => ({foo: x, bar: y}) const result = zipWith(foobar)(array1, array2) console.log(result) 
 .as-console-wrapper {height: 100vh !important;} 

Posible extensions 可能的扩展

This function has at least one drawback: if the lists are different lengths, your function might have to handle possible undefined values in either of its parameters. 此函数至少有一个缺点:如果列表的长度不同,则您的函数可能必须处理其两个参数中可能存在的未定义值。 We could fix this by working only up to the length of the shorter list. 我们可以通过仅处理较短列表的长度来解决此问题。 This is not hard to do, but the code would not be as simple. 这并不难做到,但是代码不会那么简单。 If you are interested in that, we can work through how to do it. 如果您对此感兴趣,我们可以逐步解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM