JavaScript条件下搜索对象数组

时间:2014-11-10 11:33:30

标签: javascript

我有以下几个测验对象数组,每个测验对象都有一个popupTime键:

var quizzes = {
    "quizzes": [
        {
            "_id": "546060033ea982a04f2e1859",
            "popupTime": 2.24,
            "lectureId": 5008,
            "__v": 0,
            "questions": [
                {
                    "title": "Which is the capital of India",
                    "_id": "546060033ea982a04f2e185f",
                    "answers": [
                        {
                            "answer": "Delhi",
                            "_id": "546060033ea982a04f2e1863",
                            "correct": true
                        },
                        {
                            "answer": "Bangalore",
                            "_id": "546060033ea982a04f2e1862",
                            "correct": false
                        },
                        {
                            "answer": "Mumbai",
                            "_id": "546060033ea982a04f2e1861",
                            "correct": false
                        },
                        {
                            "answer": "Chennai",
                            "_id": "546060033ea982a04f2e1860",
                            "correct": false
                        }
                    ]
                },
                {
                    "title": "Where is housing located? ",
                    "_id": "546060033ea982a04f2e185a",
                    "answers": [
                        {
                            "answer": "Delhi",
                            "_id": "546060033ea982a04f2e185e",
                            "correct": false
                        },
                        {
                            "answer": "Bangalore",
                            "_id": "546060033ea982a04f2e185d",
                            "correct": true
                        },
                        {
                            "answer": "Mumbai",
                            "_id": "546060033ea982a04f2e185c",
                            "correct": false
                        },
                        {
                            "answer": "Chennai",
                            "_id": "546060033ea982a04f2e185b",
                            "correct": false
                        }
                    ]
                }
            ]
        },
      {
    "_id" : "5460640d1e8743ee61413690",
    "popupTime" : 169,
    "lectureId" : 5008,
    "questions" : [ 
        {
            "title" : "Is this the 2nd quiz in same lecture?",
            "_id" : "5460640d1e8743ee61413691",
            "answers" : [ 
                {
                    "answer" : "True",
                    "_id" : "5460640d1e8743ee61413693",
                    "correct" : true
                }, 
                {
                    "answer" : "False",
                    "_id" : "5460640d1e8743ee61413692",
                    "correct" : false
                }
            ]
        }
    ],
    "__v" : 0
}
    ]
};

哪种扫描整个测验对象并获得一个满足popupTime = 169的对象并将其保存在另一个对象中的最有效方法是什么?

在重复呼叫的执行时间方面有效。我将在同一阵列上多次执行相同的操作以检查条件。

2 个答案:

答案 0 :(得分:1)

我建议您使用单独的array,其目的是通过popupTime index your object。因此,创建一个单独的array

var indexArray = [];

每次在Object中插入新的quizzes时,也会将popupTime插入此数组中:

indexArray.push(yournewObject.popupTime);

然后,您可以使用Array.prototype.indexOf方法找到您的元素:

var yourSearchedObject = quizzes[indexArray.indexOf(169)];

答案 1 :(得分:1)

您可以按popupTime索引集合,以执行O(1)次查找。假设popupTime是唯一的。

function index(collection, by) {
   var cache = {}, i = 0, len = collection.length, item;
   for(; i < len; ++i) { //for is faster than forEach and it's friends
      item = collection[i];
      cache[item.key] = item;
   }

   return function findOne(key) {
       return cache[key];
   }
}

var findByPopupTime = index(quizzes.quizzes, 'popupTime');
var quiz169 = findByPopupTime(169);