如何使用jQuery使用对象内的值进一步索引对象?

时间:2016-12-08 18:55:54

标签: jquery object

我希望通过question_id将以下对象分开。基本上索引所有question_id在该索引中都是相同的。

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

将它们分成独立物体的最佳方法是什么?

更新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"}
]

完整代码

                    .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 &rarr;',
                        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()
                      })

对回答的初步回应

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您想通过question_id对它们进行分组,可以使用以下代码:

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]
        };
    });
};

用法:

var newList = list.groupBy('question_id');

这是下划线的等价物:

var newList _.groupBy( list , 'question_id');

更新:

按照问题ID对其进行分组,然后在var q_id = 40;

之后
var itemsWithSameQuestion = newList.filter(function(v) {
     return v.key == q_id; // Filter out the appropriate one
})

更新2:

必须将el.groupByField更改为el[groupByField],最后将过滤条件中的v.id更改为v.key

请参阅plunkr here