ASN ASN - 3 months ago 9
Node.js Question

How to merge multiple array in JavaScript?

I have multiple arrays and I want to merge the data of all the array into one array of objects .
All the data will be merged on the basis of the key so how to make one production level code so that it will merge all the data.

results[0]=
[ {categoryGroupID: '1231',categoryID: '12311',categoryName: 'External'},
{categoryGroupID: '1231',categoryID: '12312',categoryName: 'Internal'},
{categoryGroupID: '1231',categoryID: '141414141',categoryName: ''},
{categoryGroupID: '1232',categoryID: '12321',categoryName: 'Style'},
{categoryGroupID: '1233',categoryID: '123222',categoryName: ''},
{categoryGroupID: '1233',categoryID: '12331',categoryName: 'Customer Satisfaction'}]

results[1]=
[ { categoryGroupID: '1231',categoryGroupName: 'Store ambience'},
{ categoryGroupID: '1232',categoryGroupName: 'Communication'},
{categoryGroupID: '1233',categoryGroupName: 'Overall Satisfaction'},
{categoryGroupID: '12331',categoryGroupName: null}]


results[2]=
[ {categoryID: '12311',questionID: '12311111',questionName: 'tell me snything'},
{categoryID: '12312',questionID: '12311113',questionName: 'whatever'},
{categoryID: '12311',questionID: '123ques',questionName: 'ashjsh'},
{categoryID: '12311',questionID: '123test',questionName: null}]

results[3]=
[ { questionID: '12311113',choiceID: '1231111111',choiceValue: 'good'},
{ questionID: '12311113',choiceID: '1231111112',choiceValue: 'very good'},
{questionID: '12311113',choiceID: '12311113113',choiceValue: 'bad'}]


MY output should like this so how to do it

"categoryGroup":
[
{"categoryGroupID": "1231","categoryGroupName": "Store ambience",
"category": [
{"categoryID": "12312","categoryName": "Internal",

"question": [
{"questionID": "12311113","questionName": "whatever",
"choice": [
{"choiceID": "1231111111","choiceValue": "good"},
{"choiceID": "1231111112","choiceValue": "very good"},
{"choiceID": "12311113113","choiceValue": "bad",}
],
}]
},
{"categoryID": "12311","categoryName": "External",

"question": [
{"questionID": "12311111","questionName": "tell me snything",},
{"questionID": "123ques","questionName": "ashjsh",},
{"questionID": "123test","questionName": null,}
]},
{"categoryID": "141414141","categoryName": "",}]
},

{"categoryGroupID": "1232","categoryGroupName": "Communication",
"category": [
{"categoryID": "12321","categoryName": "Style"}]
},

{"categoryGroupID": "1233","categoryGroupName": "Overall Satisfaction",
"category": [
{"categoryID": "123222","categoryName": "",},
{"categoryID": "12331","categoryName": "Customer Satisfaction",}]
},

{"categoryGroupID": "12331","categoryGroupName": null}
]

Answer

Without going into the depths of all this and how you will pretty much need to manually handle all cases here is an example of how it can work (probably not should work).

This is based on the indexes (0, 1, 2, 3) of results will always be the same and employs the filter and map Array functions. For performance I am not sure how well this will work in a large scenario.

var results = {};

results[0] =
    [{ categoryGroupID: '1231', categoryID: '12311', categoryName: 'External' },
    { categoryGroupID: '1231', categoryID: '12312', categoryName: 'Internal' },
    { categoryGroupID: '1231', categoryID: '141414141', categoryName: '' },
    { categoryGroupID: '1232', categoryID: '12321', categoryName: 'Style' },
    { categoryGroupID: '1233', categoryID: '123222', categoryName: '' },
    { categoryGroupID: '1233', categoryID: '12331', categoryName: 'Customer Satisfaction' }];

results[1] =
  [{ categoryGroupID: '1231', categoryGroupName: 'Store ambience' },
    { categoryGroupID: '1232', categoryGroupName: 'Communication' },
    { categoryGroupID: '1233', categoryGroupName: 'Overall Satisfaction' },
    { categoryGroupID: '12331', categoryGroupName: null }];


results[2] =
    [{ categoryID: '12311', questionID: '12311111', questionName: 'tell me snything' },
    { categoryID: '12312', questionID: '12311113', questionName: 'whatever' },
    { categoryID: '12311', questionID: '123ques', questionName: 'ashjsh' },
    { categoryID: '12311', questionID: '123test', questionName: null }];

results[3] =
  [{ questionID: '12311113', choiceID: '1231111111', choiceValue: 'good' },
    { questionID: '12311113', choiceID: '1231111112', choiceValue: 'very good' },
    { questionID: '12311113', choiceID: '12311113113', choiceValue: 'bad' }];



var o = {
    categoryGroup: results[1].map(function (categoryGroup) {
        var categoryGroup = {
            categoryGroupID: categoryGroup.categoryGroupID,
            categoryGroupName: categoryGroup.categoryGroupName
        };

        categoryGroup['category'] = results[0].filter(function (item) { return item.categoryGroupID == categoryGroup.categoryGroupID })
                                              .map(function (item) {
                                                  return {
                                                      categoryId: item.categoryID,
                                                      categoryName: item.categoryName,
                                                      question: results[2].filter(function (ques) { return ques.categoryID == item.categoryID })
                                                                          .map(function (ques) {
                                                                              return {
                                                                                  questionId: ques.questionID,
                                                                                  questionName: ques.questionName,
                                                                                  choice: results[3].filter(function (ch) { return ch.questionID == ques.questionID })
                                                                                                    .map(function (ch) {
                                                                                                        return {
                                                                                                            choiceID: ch.choiceID,
                                                                                                            choiceValue: ch.choiceValue
                                                                                                        }
                                                                                                    })
                                                                              }
                                                                          })
                                                  };
                                              });
        return categoryGroup;
    })
};
console.log(o);