admin管理员组

文章数量:1424437

I am making my own filter system for filtering data, and I'm building the basic functionality now, such as adding and removing filters.

Adding filters works fine, but when I play around with adding and removing, sometimes splice removes more than one filter. Why is it doing that? here is my code:

    var active_filters = [];

    var available_filters = [
        'first_name', 'last_name', 'city'
    ];

    var filters = {
        first_name: {
            title: 'First Name',
            is_active: false,
            types: [
                {
                    input: 'text'
                },

                {
                    input: 'select',
                    options: [
                        'asc', 'desc'
                    ],
                    values: [
                        'Ascending', 'Descending'
                    ]
                }
            ],

        },

        last_name: {
            title: 'Last Name',
            is_active: false,
            types: [
                {
                    input: 'text'
                },

                {
                    input: 'select',
                    options: [
                        'asc', 'desc'
                    ],
                    values: [
                        'Ascending', 'Descending'
                    ]
                }
            ],

        },

        city: {
            title: 'City',
            is_active: false,
            types: [
                {
                    input: 'text'
                },
            ],

        },
    };

    var id_filters = $("#filters");

    $(document).ready(function () {
        id_filters.html(template_filters(filters));
    });

    function template_filters(filters) {
        var html = '<select class="select_filters" id="select_filters" onchange="add_filter();">';

        html += '<option value="0">Select Filter</option>';

        for (var property in filters)
        {
            if (filters.hasOwnProperty(property))
            {
                var title = filters[property].title;
                var is_active = filters[property].is_active;
                var types = filters[property].types;

                html += '<option value="'+property+'">'+title+'</option>';
            }
        }

        html += '</select>';

        return html;
    }

    function template_show_filter(filter, filter_name)
    {
        var html = '<div id="filter-'+filter_name+'" class="div_filter">';

        html += '<span>'+filter.title+' <a href="javascript:void(0)" onclick="remove_filter(\''+filter_name+'\')">X</a></span>';

        html += '</div>';
        return html;
    }

    function add_filter()
    {
        var select_filters = $("#select_filters");
        var selected_filter = select_filters.val();

        if (selected_filter != 0)
        {
            if (active_filters.length == 0)
            {
                active_filters.push(selected_filter);
                id_filters.append(template_show_filter(filters[selected_filter], selected_filter));
            }
            else
            {
                if (active_filters.indexOf(selected_filter) === -1)
                {
                    active_filters.push(selected_filter);
                    id_filters.append(template_show_filter(filters[selected_filter], selected_filter));
                }
            }


        }
    }

    function remove_filter(filter_name)
    {
        var index = active_filters.indexOf(filter_name);



        if (index >= 0)
        {
            var id = $("#filter-"+filter_name);
            id.remove();

            active_filters.splice(index); // here, it removes more than one
        }
    }

I am making my own filter system for filtering data, and I'm building the basic functionality now, such as adding and removing filters.

Adding filters works fine, but when I play around with adding and removing, sometimes splice removes more than one filter. Why is it doing that? here is my code:

    var active_filters = [];

    var available_filters = [
        'first_name', 'last_name', 'city'
    ];

    var filters = {
        first_name: {
            title: 'First Name',
            is_active: false,
            types: [
                {
                    input: 'text'
                },

                {
                    input: 'select',
                    options: [
                        'asc', 'desc'
                    ],
                    values: [
                        'Ascending', 'Descending'
                    ]
                }
            ],

        },

        last_name: {
            title: 'Last Name',
            is_active: false,
            types: [
                {
                    input: 'text'
                },

                {
                    input: 'select',
                    options: [
                        'asc', 'desc'
                    ],
                    values: [
                        'Ascending', 'Descending'
                    ]
                }
            ],

        },

        city: {
            title: 'City',
            is_active: false,
            types: [
                {
                    input: 'text'
                },
            ],

        },
    };

    var id_filters = $("#filters");

    $(document).ready(function () {
        id_filters.html(template_filters(filters));
    });

    function template_filters(filters) {
        var html = '<select class="select_filters" id="select_filters" onchange="add_filter();">';

        html += '<option value="0">Select Filter</option>';

        for (var property in filters)
        {
            if (filters.hasOwnProperty(property))
            {
                var title = filters[property].title;
                var is_active = filters[property].is_active;
                var types = filters[property].types;

                html += '<option value="'+property+'">'+title+'</option>';
            }
        }

        html += '</select>';

        return html;
    }

    function template_show_filter(filter, filter_name)
    {
        var html = '<div id="filter-'+filter_name+'" class="div_filter">';

        html += '<span>'+filter.title+' <a href="javascript:void(0)" onclick="remove_filter(\''+filter_name+'\')">X</a></span>';

        html += '</div>';
        return html;
    }

    function add_filter()
    {
        var select_filters = $("#select_filters");
        var selected_filter = select_filters.val();

        if (selected_filter != 0)
        {
            if (active_filters.length == 0)
            {
                active_filters.push(selected_filter);
                id_filters.append(template_show_filter(filters[selected_filter], selected_filter));
            }
            else
            {
                if (active_filters.indexOf(selected_filter) === -1)
                {
                    active_filters.push(selected_filter);
                    id_filters.append(template_show_filter(filters[selected_filter], selected_filter));
                }
            }


        }
    }

    function remove_filter(filter_name)
    {
        var index = active_filters.indexOf(filter_name);



        if (index >= 0)
        {
            var id = $("#filter-"+filter_name);
            id.remove();

            active_filters.splice(index); // here, it removes more than one
        }
    }
Share Improve this question asked Oct 5, 2017 at 20:56 M. MarM. Mar 1372 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Please have a look at MDN web docs – Array.prototype.splice().

If you want to remove only one item, you should call .splice(index, 1).

If you don’t specify the second argument, “then all of the elements beginning with start index on through the end of the array will be deleted.”

This is because you are splicing at index.

active_filters.splice(index);

This will remove all elements after the index value.

本文标签: javascriptSplice removes more than one indexStack Overflow