简体   繁体   English

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

[英]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.

相关问题 类型错误:无法读取未定义的属性“first” - TypeError: Cannot read property 'first' of undefined Uncaught typeError:无法读取未定义的属性-对象 - Uncaught typeError: Cannot read property of undefined - object 未捕获的类型错误:无法读取未定义的属性“对象” - Uncaught TypeError: Cannot read property 'object' of undefined toDataURL返回错误“未捕获的TypeError:无法读取未定义的属性&#39;0&#39;” - toDataURL returns error “Uncaught TypeError: Cannot read property '0' of undefined ” md-autocomplete 返回 TypeError:无法读取未定义的属性“then” - md-autocomplete returns TypeError: Cannot read property 'then' of undefined NodeJS :: TypeError:无法读取未定义的属性“first_name” - NodeJS :: TypeError: Cannot read property 'first_name' of undefined 类型错误:无法读取未定义的 discord js 的属性“first” - TypeError: Cannot read property 'first' of undefined discord js jQuery未捕获的TypeError无法读取首页上未定义的属性“ left” - jquery uncaught TypeError cannot read property 'left' of undefined on first page 节点JS | TypeError:无法读取未定义的属性“ first_name” - Node JS | TypeError: Cannot read property 'first_name' of undefined 错误类型错误:无法读取未定义的属性“first_name” - ERROR TypeError: Cannot read property 'first_name' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM