I have one question list (q_arr
) and the user can either skip or attempt that question. In the end of last question, I display all question with their selected answer and "N/A" if not attempted.
Attempted question list is r_arr
, where q
property is the question number from q_arr
list (q:1
means the first element of q_arr
).
(function() {
let r_arr = [{q:1, a:true},{q:3, a:false}];
let q_arr = [
{ q: 'one', o: [true, false] },
{ q: 'two', o: [true, false] },
{ q: 'three', o: [true, false] },
{ q: 'four', o: [true, false] }
];
//desired output: [{q:"one", a: true}, {q:"two", a: "N/A"}, {q:"three", a: false}, {q:"four", a:"N/A"}]
let op = q_arr.filter((s, idx, arr) => {
// console.log("s", s, idx, arr);
let id = idx + 1;
r_arr.map((f) => {
if (id === f.q) {
f.q = s.q;
}
});
});
console.log("r_arr", r_arr);
})();
Now I again need to filter with q_arr
and change to get not attempted question list and merge with this new r_arr
and this seems a lengthy way.
Kindly suggest a way to solve this problem in fewer lines in ES6.
1 Answer 1
If you index the answers to the index of the questions you can do this:
let r_arr = {
0: true,
3: false
};
let q_arr = [
{ q: 'some question one', o: [true, false] },
{ q: 'some question two', o: [true, false] },
{ q: 'some question three', o: [true, false] },
{ q: 'some question four', o: [true, false] }
];
var a = q_arr.map((q, i) => ({'q': q.q, 'o': i in r_arr ? r_arr[i] : 'na'}) )
console.log(a)
You'll need to account for the fact that js arrays are zero indexed.
-
\$\begingroup\$ Thank you very much. I am not clear on second thing, what you suggested that I need to re-write
q_arr = [[ true, false], [true,false]...]
then where do I define the question text? \$\endgroup\$xkeshav– xkeshav2017年11月24日 20:54:22 +00:00Commented Nov 24, 2017 at 20:54 -
\$\begingroup\$ I'm sorry, I didn't put it together that that was the text. I thought you were just indexing the questions. Let me post an edit. \$\endgroup\$Mark M– Mark M2017年11月24日 20:55:27 +00:00Commented Nov 24, 2017 at 20:55
-
\$\begingroup\$ ok. But you have suggested the next stage when I get the new
r_arr
. I am looking to change the way I get the newr_arr
or just map with oldr_arr
andq_arr
\$\endgroup\$xkeshav– xkeshav2017年11月24日 20:58:08 +00:00Commented Nov 24, 2017 at 20:58 -
\$\begingroup\$ If i store all skipped question that will make unnecessary request to store the data in db and page will not get next question till it is not stored, lthough it took very less seconds but why we burden un necessarily to db. I am using html local storage \$\endgroup\$xkeshav– xkeshav2017年11月24日 20:59:43 +00:00Commented Nov 24, 2017 at 20:59
-
1\$\begingroup\$ Instead of
r_arr = {0: true, 3: false};
I'd suggestr_arr = {0: 0, 3: 1};
where the property value is the index of the answer. Of course for boolean answer choices this doesn't matter, but for more complicate choices, e.g. text fields, this might be a cleaner design. \$\endgroup\$le_m– le_m2017年11月24日 21:05:08 +00:00Commented Nov 24, 2017 at 21:05
// here it run the loops for the 12 times or more, based on q_arr
make the code difficult to review. A fully working demonstration with your real code would be preferable. (Press Ctrl-M in the question editor to make a demo.) \$\endgroup\$q_arr = [{q: ..., o: ...}]
I suggest more descriptive names which highlight the role instead of the datatype such asquestions = [{text: ..., choices: ...}]
. \$\endgroup\$