从数组中删除项后为什么不重置长度? [英] Why do not reset length after delete item from array ?

查看:72
本文介绍了从数组中删除项后为什么不重置长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究MVC 5项目。



我尝试了什么:



这里我试图删除json对象。



I am working on MVC 5 project.

What I have tried:

Here I trying to remove json object.

var hidJson = JSON.parse(hidvalue);      
        $.each(hidJson.Form5.Projects, function (i, obj) {
            if (obj != null) {
                if (obj.idPartnerProject == idPartnerProject) {

                    delete hidJson.Form5.Projects[i];              
                }
            }
        });





但是在删除项目后删除了但是数组的长度没有重置。我怎样才能做到这一点 ?这导致'null'关怀数组元素问题。请查看下图。

[ ^ ]



请帮帮我...



But after delete the item deleted but the length of array is not resetting. How can I do this ? This causing 'null' caring array element issue. please look upon below image.
[^]

Please help me...

推荐答案

.each (hidJson.Form5.Projects, function (i,obj){
if (obj! = null ){
if (obj.idPartnerProject == idPartnerProject){

delete hidJson.Form5.Projects [i];
}
}
});
.each(hidJson.Form5.Projects, function (i, obj) { if (obj != null) { if (obj.idPartnerProject == idPartnerProject) { delete hidJson.Form5.Projects[i]; } } });





但是在删除项目后删除但是数组的长度没有重置。我怎样才能做到这一点 ?这导致'null'关怀数组元素问题。请查看下图。

[ ^ ]



请帮帮我...



But after delete the item deleted but the length of array is not resetting. How can I do this ? This causing 'null' caring array element issue. please look upon below image.
[^]

Please help me...


我已经改变了这样的代码,



I have changed the code like this,

hidJson.Form5.Projects.splice(i, 1);





并删除此代码:



and removed this code:

delete hidJson.Form5.Projects[i];  





现在阵列刷新!!!



Now the array refreshed !!!


这篇关于从数组中删除项后为什么不重置长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆