[英]Splicing first object returns TypeError: Cannot read property of undefined
I have an array like this:我有一个这样的数组:
var arrSession = [ { name: 'Product 01', groupID: '50303', delivery: 'mail'}, { name: 'Product 02', groupID: '50403', delivery: 'bike'} ]
And this Loop to delete specific objects:这个循环删除特定对象:
for(var i=0, len=arrSession.length; i<len; i++) {
if (arrSession[i].groupID == getGroupID && arrSession[i].delivery == getDelivery) {
arrSession.splice(i, 1);
}
}
If I'm deleting the last object, all works fine:如果我删除最后一个 object,一切正常:
var getGroupID = 50403;
var getDelivery = bike;
But if I'm deleting the first object:但是如果我删除第一个 object:
var getGroupID = 50303;
var getDelivery = mail;
I get an error:我得到一个错误:
TypeError: Cannot read property 'groupID' of undefined
Why so and how to solve it?为什么会这样以及如何解决?
Edit:编辑:
If there is only one object all is fine too.如果只有一个 object 也可以。
var arrSession = [ { name: 'Product 01', groupID: '50303', delivery: 'mail'} ]
I think this is because when the loop starts, it will go up to index 1. After you delete index 0, your loop will still try to run and search for index 1, the second object, which has already been moved.我认为这是因为当循环开始时,它将 go 到索引 1。删除索引 0 后,您的循环仍将尝试运行并搜索索引 1,即第二个 object,它已被移动。 If you put a
break
keyword in the if statement, the error should be fixed.如果在 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;
}
}
Pretty simple, you iterate from the start of the array and you splice the array on found.非常简单,您从数组的开头开始迭代,然后拼接找到的数组。 The array is now shorter than the stored length and any access goes wrong.
该数组现在比存储的长度短,任何访问都会出错。
You could loop from the end instead.您可以改为从末尾开始循环。
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);
Try it converting 'groupId' to integer format.尝试将“groupId”转换为 integer 格式。
ex-:前任-:
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
mutates the array. Array.splice
改变数组。 If you remove the first element, the array length decreases by 1 but in the loop it still tries to access the next element which may or may not be undefined
.如果删除第一个元素,数组长度会减少 1,但在循环中它仍会尝试访问下一个元素,它可能是也可能不是
undefined
。 Here since there are only 2 elements the arrSession[1]
is undefined
.这里因为只有 2 个元素,所以
arrSession[1]
是undefined
。
Instead use filter like this:而是使用这样的过滤器:
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)
Hope this helps !希望这可以帮助 !
Short Answer : Due to the for loop syntax.简短回答:由于for循环语法。
Initialization
happens only once
. Initialization
只发生once
。 You missed to update len
after splicing
.你错过了
splicing
后更新len
。
for ([initialExpression]; [condition]; [incrementExpression]) statement
for ([initialExpression]; [condition]; [incrementExpression]) 语句
Solution : As already mentioned in the other answers, you can break
the loop (which will work if you are spicing only one item).解决方案:正如其他答案中已经提到的,您可以
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.