-1

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}
]
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
ASN
  • 3
  • 2
  • I don't think there is any out of the box solution for this in vanilla JS – Flying Gambit Aug 17 '16 at 06:36
  • Then how to solve this by using map or foreach or any other concept can you help me to solve this? – ASN Aug 17 '16 at 06:39
  • Take a look at that : http://stackoverflow.com/questions/17500312/is-there-some-way-i-can-join-the-contents-of-two-javascript-arrays-much-like-i, maybe it could help – boehm_s Aug 17 '16 at 06:43
  • @ASN, I think, you should give it a try yourself, if you still find it difficult, you can post the code that you have tried and we can work on it from there. – Flying Gambit Aug 17 '16 at 06:45
  • I have tried in differnt ways but not able to do it – ASN Aug 17 '16 at 06:48

2 Answers2

1

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);
Nico
  • 12,493
  • 5
  • 42
  • 62
-3

See this -> How to merge two arrays in Javascript and de-duplicate items

var array1 = ["1","2"];
var array2 = ["3", "4"];

var array3 = array1.concat(array2); // Merges both arrays
// [ '1', '2', '3', '4' ] 

for the Apache Commons Lang Array Utils see this -> How can I concatenate two arrays in Java?

js versions are dojo http://dojotoolkit.org/reference-guide/1.10/dojo/_base/array.html and util-array https://www.npmjs.com/package/util-array

This is another dojo example http://dojo-toolkit.33424.n3.nabble.com/How-to-concatenate-arrays-in-Dojo-td3744293.html

http://wiki.servicenow.com/index.php?title=ArrayUtil

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34