Reputation: 925
I am looking to separate the following object out by the question_id. Basically index where all question_id are the same within that index.
Array[13]
0
:
Object
answer:
"Water leaking"
appliance_id:
16
created_at:
"2016-11-03 22:32:49"
id:
32
question_id:
40
updated_at:
"2016-11-03 22:32:49"
__proto__:
Object
1
:
Object
answer:
"Front Loading"
appliance_id:
16
created_at:
"2016-11-03 22:33:16"
id:
33
question_id:
41
updated_at:
"2016-11-03 22:33:16"
__proto__:
Object
2
:
Object
answer:
"Yes"
appliance_id:
16
created_at:
"2016-11-03 22:33:23"
id:
34
question_id:
42
updated_at:
"2016-11-03 22:33:23"
__proto__:
Object
3
:
Object
answer:
"No"
appliance_id:
16
created_at:
"2016-11-03 22:33:30"
id:
35
question_id:
42
updated_at:
"2016-11-03 22:33:30"
__proto__:
Object
How would be the best way to separate them out into a separated object?
UPDATE JSON
[
{"id":32,"appliance_id":16,"question_id":40,"answer":"Water leaking","created_at":"2016-11-03 22:32:49","updated_at":"2016-11-03 22:32:49"},
{"id":33,"appliance_id":16,"question_id":41,"answer":"Front Loading","created_at":"2016-11-03 22:33:16","updated_at":"2016-11-03 22:33:16"},
{"id":34,"appliance_id":16,"question_id":42,"answer":"Yes","created_at":"2016-11-03 22:33:23","updated_at":"2016-11-03 22:33:23"},
{"id":35,"appliance_id":16,"question_id":42,"answer":"No","created_at":"2016-11-03 22:33:30","updated_at":"2016-11-03 22:33:30"},
{"id":36,"appliance_id":16,"question_id":43,"answer":"Yes","created_at":"2016-11-03 22:33:38","updated_at":"2016-11-03 22:33:38"},
{"id":37,"appliance_id":16,"question_id":43,"answer":"No","created_at":"2016-11-03 22:33:44","updated_at":"2016-11-03 22:33:44"},
{"id":38,"appliance_id":16,"question_id":44,"answer":"Yes","created_at":"2016-11-03 22:33:53","updated_at":"2016-11-03 22:33:53"},
{"id":39,"appliance_id":16,"question_id":44,"answer":"No","created_at":"2016-11-03 22:34:02","updated_at":"2016-11-03 22:34:02"},
{"id":40,"appliance_id":16,"question_id":45,"answer":"Just happened","created_at":"2016-11-03 22:34:16","updated_at":"2016-11-03 22:34:16"},
{"id":41,"appliance_id":16,"question_id":46,"answer":"1-3 years ago","created_at":"2016-11-03 22:34:30","updated_at":"2016-11-03 22:34:30"},
{"id":42,"appliance_id":16,"question_id":47,"answer":"Yes","created_at":"2016-11-03 22:34:35","updated_at":"2016-11-03 22:34:35"},
{"id":43,"appliance_id":16,"question_id":47,"answer":"No","created_at":"2016-11-03 22:34:39","updated_at":"2016-11-03 22:34:39"},
{"id":44,"appliance_id":16,"question_id":46,"answer":"3-5 years ago","created_at":"2016-11-03 23:20:25","updated_at":"2016-11-03 23:20:25"}
]
FULL CODE
.done(function(response){
var questions = response.questions;
var answers = response.answers;
console.log(response.questions);
console.log(response.answers);
JSON.stringify(answers);
var question = $.map(response.questions, function (item) {
return item.question;
});
var question_id = $.map(response.questions, function (item) {
return item.id;
});
console.log(question);
console.log(question_id);
var q_id = 40;
// console.log(question);
var n = $( question ).length;
var list = [];
for (var i = 1; i <= n; i++) {
list.push(i);
}
swal.setDefaults({
input: 'radio',
confirmButtonText: 'Next →',
showCancelButton: true,
animation: false,
progressSteps: list
})
// Get range of question indexes
var i = 0;
var hi = n - 1;
console.log(i);
console.log(hi);
// Sweet alert- Answers radio button
// inputOptions can be an object or Promise
var inputOptions = new Promise(function (resolve) {
setTimeout(function () {
resolve({
// ANSWERS TO GO IN HERE BY QUESTION
})
}, 1000)
})
var step = [
{
title: '',
input: 'radio',
inputOptions: inputOptions,
},
]
var steps = question.map((title) => {
var clone = Object.assign({}, step);
clone.title = title;
return clone;
});
console.log(steps);
swal.queue(steps).then(function (result) {
swal.resetDefaults()
swal({
title: 'All done!',
html:
'Your answers: <pre>' +
JSON.stringify(result) +
'</pre>',
confirmButtonText: 'Lovely!',
showCancelButton: false
})
}, function () {
swal.resetDefaults()
})
Initial response to answer
Upvotes: 0
Views: 45
Reputation: 5719
If you want to group them by question_id you can use below code:
Array.prototype.groupBy = function(groupByField) {
var groups = {};
this.forEach(function(el) {
var key = el[groupByField];
if (key in groups == false) {
groups[key] = [];
}
groups[key].push(el);
});
return Object.keys(groups).map(function(key) {
return {
key: key,
values: groups[key]
};
});
};
And the usage:
var newList = list.groupBy('question_id');
Here is the underscore equivalent:
var newList _.groupBy( list , 'question_id');
UPDATE:
Once you grouped them by question_id then after your var q_id = 40;
:
var itemsWithSameQuestion = newList.filter(function(v) {
return v.key == q_id; // Filter out the appropriate one
})
UPDATE 2:
Had to change el.groupByField
to el[groupByField]
and at last change the v.id
in the filter to v.key
.
See the plunkr here.
Upvotes: 1