gpt4 book ai didi

javascript - 拼接留下记录

转载 作者:行者123 更新时间:2023-11-29 19:15:05 25 4
gpt4 key购买 nike

为什么我下面的代码留下一条记录?如果我的 Id=40,它应该从 vm.events 中删除所有 3 条记录

vm.events = [
{
"studentId": "40",
"studentName": "P Arjun",
},
{
"studentId": "40",
"studentName": "P Arjun",
},
{
"studentId": "40",
"studentName": "P Arjun",
}
];

vm.setSelectedStudent = function (Id) {
vm.stdListCope = angular.copy(vm.events);
for (var i in vm.stdListCope) {
if (vm.stdListCope[i]['studentId'] == Id) {
vm.stdListCope.splice(i, 1);
}
}
};

最佳答案

您的代码中存在错误。

当它为 i=0 运行时,vm.stdListCope.splice(0, 1); 将数组切片为具有单个条目的数组。

所以,在 i=0 之后,vm.stdListCope[{"studentId": "40", "studentName": "P Arjun", }]

但是,当循环运行 i=1i =2 时,vm.stdListCope[i]['studentId'] == Id 不会为真,因为没有对应于索引 i=1i=2 的条目,因为它的长度 = 1。

这就是为什么您只剩下 1 个条目的原因。

关于javascript - 拼接留下记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35942325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com