- 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-21-2019 05:07 AM
Why wouldn't you use ArrayUtil().union()?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2019 03:34 AM
Yes, that would have worked, but I guess he wanted to have a structured array like what he posted in original question, like Questions from 1st Array and then followed by Answered from 2nd Array.
That is the only reason to write this code 😉