简体   繁体   English

使用深度嵌套的对象/数组 javascript 创建包含父母和孩子的数组

[英]Create array with parents and childs with deep nested object/array javascript

I want to store in a mysql table the following relations with a (DAD,SON) table that will look something like this:我想在 mysql 表中存储以下与 (DAD,SON) 表的关系,如下所示:

| DAD    || SON    |
| mike.  || june   |
| mike   || michael|
| june   || elias  |
| june   || pepe   |

and so on...等等...

I have an unknown depth nested object that will look something like this:我有一个未知深度的嵌套 object 看起来像这样:

const originalObject = {
      Dad: 'mike',
      sons: [
        {
          dad: 'june',
          sons: [
            {
              dad: 'elias',
            },
            {
              dad: 'pepe',
            },
          ],
        },
        {
          dad: 'michael',
          sons: [
            {
              dad: 'elias',
            },
            {
              dad: 'janister',
            },
            {
              dad: 'pepe',
              sons: [
                {
                  dad: 'lester',
                },
              ],
            },
          ],
        },
      ],
    };

How can i make an array that looks like this:我怎样才能制作一个看起来像这样的数组:

const desiredArray = [
    ['mike', 'june'],
    ['mike', 'michael'],
    ['june', 'elias'],
    ['june', 'elias'],
    etc,]

I have already converted the object to an array, but im not sure if it helps... The array right now is like:我已经将 object 转换为数组,但我不确定它是否有帮助......现在的数组就像:

const parsedObjectToArray =[ 'mike',[ [ 'june', [elias, pepe] ], [ 'michael', [elias, janister, pepe] ] ] ];

So, based on the originalObject or the parsedObjectToArray, how can i transform one of this to the desiredArray??那么,基于 originalObject 或 parsedObjectToArray,我如何将其中一个转换为所需的Array? I have searched but i struggling hard!我已经搜索过,但我很努力! Thanks!谢谢!

You can use a recursive function that dives down the object and adds to an overall dads array.您可以使用递归 function 向下潜入 object 并添加到整个dads数组。

const dads = [];

function addToDads(obj) {
  obj.sons.forEach(son => {
    dads.push([obj.dad, son.dad]);
    if (son.sons) {
      addToDads(son);
    }
  })
}

addToDads(originalObject);

console.log(dads);

See it in action below:请参阅下面的实际操作:

 const originalObject = { dad: 'mike', sons: [ { dad: 'june', sons: [ { dad: 'elias', }, { dad: 'pepe', }, ], }, { dad: 'michael', sons: [ { dad: 'elias', }, { dad: 'janister', }, { dad: 'pepe', sons: [ { dad: 'lester', }, ], }, ], }, ], }; const dads = []; function addToDads(obj) { obj.sons.forEach(son => { dads.push([obj.dad, son.dad]); if (son.sons) { addToDads(son); } }) } addToDads(originalObject); console.log(dads);

You could take a recursion with Array#flatMap .您可以使用Array#flatMap进行递归。

 const getRelatives= ({ dad, sons = [] }) => sons.flatMap(son => [ [dad, son.dad], ...getRelatives(son) ]), object = { dad: 'mike', sons: [{ dad: 'june', sons: [{ dad: 'elias' }, { dad: 'pepe' }] }, { dad: 'michael', sons: [{ dad: 'elias' }, { dad: 'janister' }, { dad: 'pepe', sons: [{ dad: 'lester' }] }] }] }, result = getRelatives(object); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

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