- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 04:38 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 04:54 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 04:41 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 04:54 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 11:04 PM
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"}]},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2023 04:56 AM - edited 02-06-2023 03:42 AM
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!