简体   繁体   English

将多个 arrays 中的值存储到一个数组中 (JavaScript/Reactjs)

[英]Store values from multiple arrays into one array (JavaScript/Reactjs)

I have an array, which is having arrays as it's values.我有一个数组,它的值是 arrays。 What I need is an output that is having all array values.我需要的是具有所有数组值的 output。

Note : input array and it's elements(arrays) are not of fixed length.注意:输入数组及其元素(数组)不是固定长度的。

Input :输入

array = 
[ 
  [a,b],
  [c,d,e],
  [f,g]
]

Output : Output

[
 {
  Header : a
  Children : 
  [
   {
    Header : c
    Children : 
     [
      Header : f
     ]
   },
  ]
 },
 {
  Header : a
  Children : 
  [
   {
    Header : c
    Children : 
     [
      Header : g
     ]
   },
  ]
 },
 {
  Header : a
  Children : 
  [
   {
    Header : d
    Children : 
     [
      Header : f
     ]
   },
  ]
 },
.
.
.
{
  Header : a
  Children : 
  [
   {
    Header : e
    Children : 
     [
      Header : g
     ]
   },
  ]
 },
 {
  Header : b
  Children : 
  [
   {
    Header : c
    Children : 
     [
      Header : f
     ]
   },
  ]
 },
 .
 .
 .
 .
 .
 {
  Header : b
  Children : 
  [
   {
    Header : e
    Children : 
     [
      Header : g
     ]
   },
  ]
 },
]

Basically it is like,基本上是这样的,

[a,c,f],
[a,c,g],
[a,d,f],
[a,d,g],
[a,e,f],
[a,e,g],
[b,c,f],
[b,c,g],
[b,d,f],
[b,d,g],
[b,e,f],
[b,e,g]

I have tried using for loop but couldn't get the desired output.我曾尝试使用 for 循环,但无法获得所需的 output。 Is there any way that we can achieve that result?有什么方法可以达到这个结果吗?

You can create a Cartesian product of the 2D array first.您可以先创建二维数组的笛卡尔积 Then, use reduceRight to create a nested object for each array in the Cartesian product然后,使用reduceRight为笛卡尔积中的每个数组创建一个嵌套的 object

 const data = [ ["a", "b"], ["c", "d", "e"], ["f", "g"] ] const cartesian = data.reduce((acc, curr) => acc.flatMap(c => curr.map(n => [].concat(c, n))) ) const tree = arr => arr.reduceRight((acc, Header) => acc? ({ Header, Children: [acc] }): ({ Header }), null) const output = cartesian.map(tree) console.log(JSON.stringify(cartesian)) console.log(output)

function join(arrayOfArrays) {
    let current = [[]];
    for (let array of arrayOfArrays) {
        current = current.flatMap(c=>array.map(a=>[...c, a]));
    }
    return current;
}

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

相关问题 ReactJS 如何从 arrays 阵列更改为单个阵列 - ReactJS How to change from array of arrays to one single array 如何将多个值存储到一个可重用变量-reactJs - How could I store multiple values to one reusable variable - reactJs 如何从嵌套在 object 内的数组中的 object 中提取值并将其存储在不同的 arrays Z53C6C951F4E558B9DFF298869 - how to extract values from an object nested inside array inside object and store it in different arrays JAVASCRIPT ReactJS从json数据内部数组设置状态-多个数组 - ReactJS Set state from json data inner array - Multiple Arrays 在数组 Javascript 中存储多个单选按钮值 - Store multiple radio button values in an array Javascript 如何在 JavaScript 中存储一个键和多个值数组? - How to store a key and multiple values array in JavaScript? JavaScript - 将对象的多个 arrays 转换为基于水平值的 arrays 数组 - JavaScript - Convert multiple arrays of an objects to horizonal values based array of arrays 如何将列表中的值存储到ReactJS中的对象数组中 - How to store values from a list into an array of objects in ReactJS 从 javaScript 中的多个数组值创建一个字符串 - Creating one single string from multiple values of array in javaScript 从JavaScript中的多个数组获取值 - Get values from multiple arrays in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM