如何从jquery中的列表中删除对象

时间:2015-11-05 10:26:46

标签: javascript jquery

我正在遍历列表,我想在遍历时根据条件删除对象,以便在我的cshtml页面下不显示对象信息。即如果我的结束月份值小于isValidActivityPosition,那么我不想在ui中显示该元素值:

以下是我要做的事情:**

的.js

//the "priorityObj.UpcomingActivities" has two objects under it [0] and [1]
if (priorityObj.UpcomingActivities() != "") {
    $.each(priorityObj.UpcomingActivities(), function (i, v) {
        monthPosition = v.ActivityDateToDisplay().split(',')[0].substring(0, 3).trim();           

        var isValidActivityPosition = jQuery.inArray(monthPosition, priorityMonth);
        //if the variable isValidActivityPosition is greater than endMonth
        //than remove the object which is currently traversed upon in the $.each
        if (isValidActivityPosition > endMonth) {
            v.remove();
        }

    });
}

cshtml页面

<div data-bind="foreach:UpcomingActivities()">
            <div class="priority-icon">

我不知道应该用什么来代替 v.remove ,以便删除当前元素并且不应该在UI中显示?

2 个答案:

答案 0 :(得分:0)

尝试:

 temp = priorityObj.UpcomingActivities();//create temporar object
if (priorityObj.UpcomingActivities() != "") {
    $.each(priorityObj.UpcomingActivities(), function (i, v) {
        monthPosition = v.ActivityDateToDisplay().split(',')[0].substring(0, 3).trim();           

        var isValidActivityPosition = jQuery.inArray(monthPosition, priorityMonth);
        //if the variable isValidActivityPosition is greater than endMonth
        //than remove the object which is currently traversed upon in the $.each
        if (isValidActivityPosition > endMonth) {
            temp.splice(i,1);//remove data from it
        }

    });
}

答案 1 :(得分:0)

//the "priorityObj.UpcomingActivities" has two objects under it [0] and [1]
if (priorityObj.UpcomingActivities() != "") {
    $.each(priorityObj.UpcomingActivities(), function (i, v) {
        monthPosition = v.ActivityDateToDisplay().split(',')[0].substring(0, 3).trim();           

        var isValidActivityPosition = jQuery.inArray(monthPosition, priorityMonth);
        //if the variable isValidActivityPosition is greater than endMonth
        //than remove the object which is currently traversed upon in the $.each
        if (isValidActivityPosition > endMonth) {
            $(this).remove();
        }

    });
}