简体   繁体   English

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

[英]how to remove an object from a list in jquery

I am traversing on a list and I want to remove an object based on a condition while traversing so that the object information is not shown under my cshtml page. 我在列表上遍历,并且想在遍历时根据条件删除一个对象,这样该对象信息不会显示在我的cshtml页面下。 ie if my end month value is smaller than the isValidActivityPosition than I dont want to show that element value in the ui: 即如果我的月末值小于isValidActivityPosition而不是我不想在ui中显示该元素值:

Below is what I am trying to do:** 以下是我要执行的操作:**

.js .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 page cshtml页面

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

I dont know what should I write in place of v.remove so that the current element is removed and should not be shown in the UI? 我不知道应该用什么来代替v.remove,以便删除当前元素并且不应在UI中显示该元素?

Try: 尝试:

 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
        }

    });
}
//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();
        }

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM