对象数组上的多个过滤器

时间:2015-02-15 12:40:06

标签: javascript arrays

我目前正在移植我的一个基于SQL过滤器的应用程序,这样每次更改过滤器时都不会查询数据库,而是从一个单一的JavaScript对象数组中过滤( "主数组")。

// console.log(array)

[Object, Object, Object, ...]
    0: Object
        date_created: "2014-12-20"
        product_id: "1"
        product_name: "name"
        type: "0"
        purchased: "1"
    1: Object
        date_created: "2014-12-21"
        product_id: "2"
        product_name: "name2"
        type: "1"
        purchased: "0"
    2: Object
        date_created: "2014-12-21"
        product_id: "3"
        product_name: "name3"
        type: "1"
        purchased: "0"

    ... etc etc

目前我有JavaScript数组,并且我有一个基本的过滤系统,我可以过滤掉结果只显示基本项目(type == 0),只显示高级项目(type == 1)或所有物品。然后,它将根据所选的产品名称,成本或日期进行排序。

这种方式的工作方式是它遍历主数组,将当前对象分配给变量,然后遍历该特定对象。如果show only basic设置为true)和key == 'type' && value == 0,那么我们将该对象推送到新数组中。在主数组循环结束时,如果新数组创建的数组仍为空,则表示未进行匹配。它只是复制主数组,然后对该数组应用一些过滤。

我的头痛,以及我无法绕过头,我该怎么做多个过滤器?对象可能是basic AND purchased,但我当前的算法会将该记录插入两次。当我考虑计算插入次数,然后执行array.splice删除其他对象时,那就是失败并且返回了不需要的结果。这是我的尝试:

function run_filter (type) {
    /*ajax_active = true;
    $('#loading').show().center();*/

    // new empty array
    var new_items = [];

    // items_length refers to the size of the master array (8 objects in total)
    for(var i = 0; i < items_length; i++) {

        // grab the current object
        var object = all_items[i];
        // grab the current size of the new array
        var new_items_length = new_items.length;
        // reset to 0
        var insert_amount = 0;

        // for each key in current object
        for(var key in object) {
            // if show only basic and hide advanced
            if (show_basic && !show_advanced) {
                if (key == 'type' && object[key] == 0) {
                    // push the object onto the new array
                    new_items.push(object);
                    // incrememnt counter by 1
                    insert_amount++;
                } 
            }
            if (show_advanced && !show_basic) {
                if (key == 'type' && object[key] == 1) {
                    new_items.push(object);
                    insert_amount++;
                } 
            }
            // if we want to hide purchased (i.e. omit records where purchased == 1
            if (hide_purchased) {
                if (key == 'purchased' && object[key] == 0) {
                    new_items.push(object);
                    insert_amount++;
                }
            }

            // if increment counter == 2 or greater (i.e. an object has been inserted twice because it is both basic and hasn't been purchased)
            if(insert_amount > 1) {
                // new array length BEFORE objects pushed onto new array (+1 to it so that we don't remove the first instance of the object) ; ... < new array length AFTER objects pushed ; increment on loop
                for(var index = new_items_length + 1; index < new_items_length + insert_amount; index++) {
                    // remove the object from the new array at the current index
                    new_items.splice(index, 1);
                }   
            }
        }
    }
}

最重要的是,如果某个项目是基本的,会被推入新阵列,但用户也购买了此记录并将过滤器设置为隐藏购买。它会显示该记录,因为它是基本的,并忽略隐藏购买的过滤器。我知道我可以拥有数百个嵌套if conditions,但这似乎效率低下......

任何帮助??!

1 个答案:

答案 0 :(得分:1)

我认为您可以使用Array.filter()来简化代码,Example创建一个新数组,其中所有元素都通过提供的函数实现的测试。:

function run_filter (type) {
    var new_items = all_items.filter(function(obj, index, array) {
        // You can do any filter you want here
        // Just focus on the filter
        return (obj.type == type) && (obj.purchased == "1");
    });

    return new_items;
}

请参阅此{{3}}