0

so I can't solve this issue. I have the following arrays:

var array1 = [
USA,
Georgia,
Atlanta
]

var array2 = [
USA,
New York
]

This is what I want:

{
    "counties": [
        {
            "name": "USA",
            "id": "1",
            "children": [
                {
                    "name": "Georgia",
                    "id": "1.1",
                    "parentId": "1",
                    "children": [
                        {
                            "name": "Atlanta",
                            "id": "1.1.1",
                            "parentId": "1.1"
                        }
                    ]
                },
                {
                    "name": "New York",
                    "id": "1.2",
                    "parentId": "1"
                }
            ]
        }
    ]
}

The trick is to not to have any duplications, if there is any duplications on the same level, they should be merged into one and have both's children under. Like USA in the example.

TheStranger
  • 1,387
  • 1
  • 13
  • 35
  • where you want to achieve such kind of objects using Vanilla JavaScript or Response from NodeJs? – Abu Sufian Mar 03 '20 at 12:04
  • 1
    What have you tried so far ? – PHPnoob Mar 03 '20 at 12:05
  • @PHPnoob The closest thing I've tried is to loop through the arrays and push the items into "countries", but i don't know how to nest the items recursively into the children array.. – TheStranger Mar 03 '20 at 12:09
  • 1
    Does this answer your question? [Node.js - Convert flat json to hierarchical json without 'parent','child' attributes](https://stackoverflow.com/questions/29921218/node-js-convert-flat-json-to-hierarchical-json-without-parent-child-attrib) – Soham Lawar Mar 03 '20 at 12:32
  • 1
    @Soham Lawar - The shape-json module which Kito has sugested looks very interesting. I'll install it and try it out, maybe that's the solution. – TheStranger Mar 03 '20 at 12:51
  • "I've tried serveral ways," SHOW Them – epascarello Mar 03 '20 at 15:09
  • @epascarello I don't understand why you have to get caught up in that instead of just helping. To make life a bit easier for you, I have removed it.. – TheStranger Mar 03 '20 at 16:22
  • @Benji No, showing what you tried means you made effort and people will help you. Maybe it was a simple mistake you made. We have no clue because you did not show it. I am giving you basic advice when you ask questions: Give examples of what you tried, they go a long way here. – epascarello Mar 03 '20 at 16:25
  • @epascarello okay thank you. I didn't keep the methods that I used because they seemed waay off. But I will use your adive in the future and post the things that i have tried. Thank you. – TheStranger Mar 04 '20 at 09:16

1 Answers1

1

I've got this transform methods. Pass any amount of arrays in parameters :

Demo:

const array1 = [
    'USA',
    'Georgia',
    'Atlanta'
]

const array2 = [
    'USA',
    'New York'
]



const transform = (...arrays) => {
    const result = [];

    arrays.forEach(array => {
        let node = result;
        let parentID = ''

        array.forEach(item => {

            const current = node.find(c => c.name === item);

            if(current){
                node = current.children
            }else{
                const newNode = {
                    name: item,
                    children: [],
                    id: parentID === '' ? (node.length+1) + '' : parentID + '.'  + (node.length+1),
                    parentID
                };

                parentID = newNode.id;
                node.push(newNode);
                node = newNode.children
            }

        })
    })

    return {counties: result}
}

const result = transform(array1, array2);

console.log(result);

// {
//     "counties": [
//         {
//             "name": "USA",
//             "id": "1",
//             "parentID": "",
//             "children": [
//                 {
//                     "name": "Georgia",
//                     "id": "1.1",
//                     "parentID": "1",
//                     "children": [
//                         {
//                             "name": "Atlanta",
//                             "id": "1.1.1",
//                             "parentID": "1.1",
//                             "children": []
//                         }
//                     ]

//                 },
//                 {
//                     "name": "New York",
//                     "children": [],
//                     "id": "2",
//                     "parentID": ""
//                 }
//             ]

//         }
//     ]
// }
cthmsst
  • 374
  • 1
  • 13