Combine two Array objects in one

Deepak Pal1
Kilo Explorer

I have 2 different array in JSON object .

Array1 = [ { "quesText": “Which shift you works in ?" }, { "quesText": " Are you a permanent employee?" }, { "quesText": "Is this your primary or secondary account?” } ]

 

Array2 = [ { "answer": "Evening" }, { "answer": "Morning" }, { "answer": "Yes" }, { "answer": "No" }, { "answer": "Primary" }, { "answer": "Secondary" } ]

 

I want these to combine this way.

How can I achieve this ?

items = [{"Questions" : "Which shift you works in ",

                                    "answer"  : ["Evening","Morning"]},

                                    {"Questions" : "Are you a permanent employee? ",

                                    "answer"  : ["Yes","No"]},

                                    {"Questions" : "Is this your primary or secondary account? ",

                                    "answer"  : ["Primary","Secondary"]}

 

My whole purpose is to use this in NG-REPEAT so it print the question first then answer and then second question.

If any other way to achieve this let me know.

1 ACCEPTED SOLUTION

Deepak Ingale1
Mega Sage

Hi,

here is the code that will give you items what you are looking for

 

Note : Please mark reply as correct if it has answered your question

var Array1 = [{
    "quesText": "Which shift you works in ?"
}, {
    "quesText": " Are you a permanent employee?"
}, {
    "quesText": "Is this your primary or secondary account?"
}];
var Array2 = [{
    "answer": "Evening"
}, {
    "answer": "Morning"
}, {
    "answer": "Yes"
}, {
    "answer": "No"
}, {
    "answer": "Primary"
}, {
    "answer": "Secondary"
}];
var items = [];

for (var i = 0; i < Array1.length; i++) {
    var temp = {};
    temp.Questions = Array1[i]['quesText'];
    temp.answer = [];
    temp.answer.push(Array2[i * 2]['answer']);
    temp.answer.push(Array2[i * 2 + 1]['answer']);
    items.push(temp);
}

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Deepak,

you need to form that array using script and cannot use arrayutil concat method

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Deepak Ingale1
Mega Sage

Hi,

here is the code that will give you items what you are looking for

 

Note : Please mark reply as correct if it has answered your question

var Array1 = [{
    "quesText": "Which shift you works in ?"
}, {
    "quesText": " Are you a permanent employee?"
}, {
    "quesText": "Is this your primary or secondary account?"
}];
var Array2 = [{
    "answer": "Evening"
}, {
    "answer": "Morning"
}, {
    "answer": "Yes"
}, {
    "answer": "No"
}, {
    "answer": "Primary"
}, {
    "answer": "Secondary"
}];
var items = [];

for (var i = 0; i < Array1.length; i++) {
    var temp = {};
    temp.Questions = Array1[i]['quesText'];
    temp.answer = [];
    temp.answer.push(Array2[i * 2]['answer']);
    temp.answer.push(Array2[i * 2 + 1]['answer']);
    items.push(temp);
}

Thanks Deepak, it worked but can you also help me to create it in this format to use efficiently in ng-repeat

items = [{"Questions" : "Which shift you works in ",
"answer : [{"value" : "Evening", "value" : "Morning"}]},

Hello,

can we combine two arrays in similar way based on their index values.

if array1 =[1,2,3]      array2 = [a,b,c]

array3 should be [1-a , 2-b, 3-c]; or

array3 = [1(a),2(b),3(c)];

 

Any answer can be appreciated.Thanks!