<script>
    //集合操作
    var list = []
    var count = 10000 //集合数据总数


    //索引方式添加
    console.time("add")
    for (i = 0; i < count; i++) {
        addItem(list, { "name": "zhangsan" + i, sex: "male", age: i + 100 })
    }
    console.timeEnd("add")


    //索引方式删除
    console.time("delete")
    removeItem(list, "age", 10000);
    console.timeEnd("delete")
    console.log(list.length)

    //索引方式查找
    console.time("search")
    var item = searchItem(list, "age", 2000);
    console.timeEnd("search")
    console.log(item)

    /**
     * 集合里查找对象
     * **/
    function searchItem(list,elem,value) {
        var item = null;
        for (i = 0; i < list.length; i++) {
            if (typeof(list[i][elem]) != "undefined" && list[i][elem] == value) {
                item = list[i]
                break;
            }
        }
        return item
    }

    /**
     * 集合里新增对象
     * **/
    function addItem(list, item) {
        try {
            list[list.length] = item;
        }
        catch (e) { }
    }

    /**
     * 集合里删除对象
     * **/
    function removeItem(list, elem, value) {
        try {
            for (i = 0; i < list.length; i++) {
                if (typeof(list[i][elem]) != "undefined" && list[i][elem] == value) {
                    list.splice(i, 1)
                }
            }
        } catch (e) { }
    }

</script>


本文转载:CSDN博客