从数组中删除已经存在的对象,否则插入

Remove already exist object from array otherwise insert

本文关键字:对象 插入 存在 数组 删除      更新时间:2023-09-26

下面的javascript代码添加对象到javascript数组。我想添加一个对象到数组时,它不存在,如果它已经存在object.rValue!= new object.rValue,那么改变old rValue=new rValue,否则相同的rVale。也将其保存在数组中。问题是对象是动态填充的。

var arr = [];
$(document).ready(function() {
    $(".rating").click(function() {
        var idx = $(this).closest('td').index();
        var userskill = {
            tech : $(this).closest('td').siblings('td.tech').text(),
            skill : $('#listTable thead th').eq(idx).text(),
            rValue : $(this).val()
        }
        validate(userskill);
    });
});
function validate(userskill) {
}

试试这个

arr.forEach(function(elem){
    if(elem.rValue==newObj.rValue)
         elem.rValue = newObj.rValue;
})