简体   繁体   English

用JavaScript乘2个数组

[英]Multiplying 2 Arrays in JavaScript

I have 2 arrays 我有2个数组

array1 = ["a","b"]
array2 = [ ["1","2","3"],["10","11","12"]]

The output I want is an array of objects 我想要的输出是对象数组

[
 {array1: a , array2: 1},
 {array1: a , array2: 2},
 {array1: a , array2: 3},

 {array1: b , array2: 10},
 {array1: b , array2: 11},
 {array1: b , array2: 12},
]

Is there a concise way to achieve this output instead of nested loops 是否有一种简洁的方法来实现此输出而不是嵌套循环

It seems like you just need two loops. 似乎您只需要两个循环。 There are many ways to do that. 有很多方法可以做到这一点。 A concise way is to reduce array1 and map array2 into the result within the outer loop: 一种简洁的方法是reduce array1并将array2 map到外部循环内的结果中:

 let array1 = ["a","b"] let array2 = [ ["1","2","3"],["10","11","12"]] let res = array1.reduce((arr, array1, i) => arr.concat(array2[i].map(array2 => ({array1, array2}))) , []) console.log(res) 

You could reduce the arrays and get the cartesian product of the outer elements of each arrays. 您可以缩小数组,并获得每个数组的外部元素的笛卡尔积。

 function getCartesian(object) { return Object.entries(object).reduce((r, [k, v]) => { var temp = []; r.forEach(s => (Array.isArray(v) ? v : [v]).forEach(w => (w && typeof w === 'object' ? getCartesian(w) : [w]).forEach(x => temp.push(Object.assign({}, s, { [k]: x })) ) ) ); return temp; }, [{}]); } var array1 = ["a", "b"], array2 = [["1", "2", "3"], ["10", "11", "12"]], result = array1.reduce((r, a, i) => r.concat(getCartesian({ array1: a, array2: array2[i] })), []) console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can just use the map() method on the second array then use another map() method on the nested arrays. 你可以只使用地图()方法中的第二阵列上,然后使用另一地图()方法在嵌套阵列。 Finally, use the indexes to group the "nth" element from the first array with the elements from the "nth" nested array together inside an object like this: 最后,使用索引将第一个数组中的“ nth”个元素与“ nth”个嵌套数组中的元素归为一个对象,如下所示:

 array1 = ["a","b"] array2 = [ ["1","2","3"],["10","11","12"]] let x = []; array2.map((e,i) => { // call function on every element in array2 with index callback e.map((f,j) => { // call function on every element in the nested arrays with index callback let obj = {}; obj["array1"] = array1[i]; obj["array2"] = f; x.push(obj); }); }); console.log(x); 

This is a similar approach to Marks Meyer answer, but using a forEach() instead of the reduce() . 这与Marks Meyer的答案类似,但是使用的是forEach()而不是reduce()

 let array1 = ["a","b", "c"]; let array2 = [["1","2","3"], ["10","11","12"]]; let res = []; array1.forEach((x, i) => { if (array2[i]) res.push(...array2[i].map(y => ({array1: x, array2: y}))); }); console.log(res); 

You can use .map() and .flat() to map each element of array2 for each element of array1 then flatten the resulting array. 您可以使用.map().flat()array1每个元素映射array2的每个元素,然后展平结果数组。

 const array1 = ["a","b"] const array2 = [ ["1","2","3"],["10","11","12"]] let res = array1.map((array1, i) => array2[i].map(array2 => ({array1, array2}))).flat() console.log(res) 

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

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