繁体   English   中英

先拼接 object 返回 TypeError: Cannot read property of undefined

[英]Splicing first object returns TypeError: Cannot read property of undefined

我有一个这样的数组:

var arrSession = [ { name: 'Product 01', groupID: '50303', delivery: 'mail'}, { name: 'Product 02', groupID: '50403', delivery: 'bike'} ]

这个循环删除特定对象:

for(var i=0, len=arrSession.length; i<len; i++) {
    if (arrSession[i].groupID == getGroupID && arrSession[i].delivery == getDelivery) {
        arrSession.splice(i, 1);
    }
}

如果我删除最后一个 object,一切正常:

var getGroupID = 50403;
var getDelivery = bike;

但是如果我删除第一个 object:

var getGroupID = 50303;
var getDelivery = mail;

我得到一个错误:

TypeError: Cannot read property 'groupID' of undefined  

为什么会这样以及如何解决?

编辑:

如果只有一个 object 也可以。

var arrSession = [ { name: 'Product 01', groupID: '50303', delivery: 'mail'} ]

我认为这是因为当循环开始时,它将 go 到索引 1。删除索引 0 后,您的循环仍将尝试运行并搜索索引 1,即第二个 object,它已被移动。 如果在 if 语句中放置一个break关键字,错误应该被修复。

for(var i=0, len=arrSession.length; i<len; i++) {
    if (arrSession[i].groupID == getGroupID && arrSession[i].delivery == getDelivery) {
        arrSession.splice(i, 1);
        break;
    }
}

非常简单,您从数组的开头开始迭代,然后拼接找到的数组。 该数组现在比存储的长度短,任何访问都会出错。

您可以改为从末尾开始循环。

 var arrSession = [{ name: 'Product 01', groupID: '50303', delivery: 'mail' }, { name: 'Product 02', groupID: '50403', delivery: 'bike' }], getGroupID = 50303, getDelivery = 'mail', i = arrSession.length; while (i--) { if (arrSession[i].groupID == getGroupID && arrSession[i].delivery == getDelivery) { arrSession.splice(i, 1); } } console.log(arrSession);

尝试将“groupId”转换为 integer 格式。

前任-:

for(var i=0, len=arrSession.length; i<len; i++) {
    if (parseInt(arrSession[i].groupID) == getGroupID && arrSession[i].delivery == getDelivery) {
        arrSession.splice(i, 1);
    }
}

Array.splice改变数组。 如果删除第一个元素,数组长度会减少 1,但在循环中它仍会尝试访问下一个元素,它可能是也可能不是undefined 这里因为只有 2 个元素,所以arrSession[1]undefined

而是使用这样的过滤器:

 var arrSession = [{ name: 'Product 01', groupID: '50303', delivery: 'mail' }, { name: 'Product 02', groupID: '50403', delivery: 'bike' }] var getGroupID = 50303; var getDelivery = 'mail'; var filtered = arrSession.filter(session => session.id.== getGroupID && session.delivery !== getDelivery) console.log(filtered)

希望这可以帮助 !

简短回答:由于for循环语法。 Initialization只发生once 你错过了splicing后更新len

for ([initialExpression]; [condition]; [incrementExpression]) 语句

解决方案:正如其他答案中已经提到的,您可以break循环(如果您只添加一个项目,这将起作用)。

 const arrSessionActual = [{ name: 'Product 01', groupID: '50303', delivery: 'mail' }, { name: 'Product 02', groupID: '50403', delivery: 'bike' }]; function removeItem(arrSession, getGroupID, getDelivery) { for (var i = 0, len = arrSession.length; i < len; i++) { if (arrSession[i].groupID == getGroupID && arrSession[i].delivery == getDelivery) { arrSession.splice(i, 1); len = arrSession.length; // This is missing } } console.log(arrSession); } removeItem(arrSessionActual.slice(), 50403, 'bike'); removeItem(arrSessionActual.slice(), 50303, 'mail');

暂无
暂无

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

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