0

I want to store in a mysql table the following relations with a (DAD,SON) table that will look something like this:

| 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:

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:

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?? I have searched but i struggling hard! Thanks!

2
  • 2
    strange family ... just dads ... Commented Mar 14, 2021 at 13:51
  • @NinaScholz yeah :D Its just to make it more simple to visualize Commented Mar 14, 2021 at 13:54

2 Answers 2

1

You can use a recursive function that dives down the object and adds to an overall dads array.

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like it is exactly this i was looking for! You are awesome! Thank you a lot!
1

You could take a recursion with 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; }

1 Comment

This answer also looks awesome! Thank you! ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.