Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向数组中添加/删除对象,并检查对象是否已存在_Javascript_Extjs_Sencha Touch - Fatal编程技术网

Javascript 向数组中添加/删除对象,并检查对象是否已存在

Javascript 向数组中添加/删除对象,并检查对象是否已存在,javascript,extjs,sencha-touch,Javascript,Extjs,Sencha Touch,我有一个任务,我需要将一个对象推入数组,在推入之前需要检查该对象是否已经存在,如果需要从数组中删除/移除该对象。 我已经编写了示例代码,但没有得到预期的输出 optionlistItemTap : function (data, index) { var record = data.getStore().getAt(index); var Id = record.raw.id; var arraysize = names.length; for (i = 0;

我有一个任务,我需要将一个对象推入数组,在推入之前需要检查该对象是否已经存在,如果需要从数组中删除/移除该对象。 我已经编写了示例代码,但没有得到预期的输出

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:函数(数据、索引){
var record=data.getStore().getAt(索引);
var Id=record.raw.Id;
var arraysize=names.length;

对于(i=0;iindexId在else中未定义,如果block

查看您的代码,我的最佳猜测是您需要:

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.
            }
        }
    }
},
optionlistItemTap:函数(数据、索引){
var record=data.getStore().getAt(index);//获取记录
var Id=record.raw.Id;//获取记录的Id
var arraysize=names.length;//获取'names'数组的长度
如果(arraysize==0){//如果`names`数组为空,
names.push(record);//将记录推送到数组中。
return;//中断函数。
}否则{//,

对于(var i=0;i),我们无法帮助您修复部分代码:在
名称
索引
,如果
,以及
记录
的内容是什么?您如何调用该函数,您希望它做什么,它做什么?此外,您缺少荣誉(
{/code>)最后一次使用
else
@sra时:不能对复杂对象使用
indexOf
。例如,在:
var a={a:1};var b=[{a:1}];
b.indexOf(a);
将返回
-1
。我将删除误导性的注释。我从未尝试以这种方式查找复杂对象,但我想你是对的。谢谢你的回答!