在数组中添加/删除对象,并检查对象是否已退出
Adding/removing an object into array and also check if object already exits
我有一个任务,我需要将一个对象推送到数组中,在推送之前需要检查对象是否已经退出,如果需要从数组中删除/移除对象。我已经编写了示例代码,但没有按预期获得输出。
optionlistItemTap : function (data, index) {
var record = data.getStore().getAt(index);
var Id = record.raw.id;
var arraysize = names.length;
for (i = 0; i <= arraysize; i++) {
if (arraysize == 0) {
names.push(record);
var indexId = names[i].raw.id
var Id = record.raw.id
break;
}
else if (indexId == Id) {
names.splice(i, 1);
break;
}
else
names.push(record);
}
},
看看你的代码,我最好的猜测是你需要这个:
optionlistItemTap : function (data, index) {
var record = data.getStore().getAt(index); // Get the record
var Id = record.raw.id; // Get the record's id
var arraysize = names.length; // Get the length of the `names` array
if (arraysize == 0) { // If the `names` array is empty,
names.push(record); // Push the record to the array.
return; // Break out of the function.
}else{ // Otherwise,
for (var i = 0; i <= arraysize; i++) { // Loop through the array,
if (names[i].raw.id == Id) { // And if names[i]'s id matches the id of the element to add,
names.splice(i, 1, record); // Replace the element in `names` with the record.
return; // Break out of the function.
}
}
}
},
indexId 在 else if block 中未定义
相关文章:
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- JS数组-检查对象值是否重复
- 如何检查数组中的对象是否等于选项值
- 使用js/jQuery检查对象(而不是元素)是否真的存在
- 如何通过json对象选项卡中的Id来检查对象是否存在
- 尝试简化检查对象键是否为true并将其推送到另一个对象
- 检查对象是否是mongo游标
- 检查对象的所有属性是否未定义
- 如何使用*ngIf检查角度2模板中的空对象
- javascript检查对象存在
- Three.js-如何检查对象是否在球体后面(不可见)
- 检查对象是否基于jquery对象
- 正在检查对象javascript中是否存在嵌套属性
- 在对象数组上使用map()检查差异
- 如何检查文字对象是否在 Javascript 数组中
- javascript对象检查在IE11中不起作用
- Javascript 对象检查
- 通过JavaScript中的dictionary对象检查单词的有效性-是否有更好的方法来引用/构建词典
- 如何将对象检查为未定义
- 如何使用 jquery 延迟对象检查元素是否在 DOM 中