2012-03-07 97 views
15

我正在建立一个简单的过滤系统,我只是想添加一个字符串数组,并删除它,如果它已经存在点击一个链接。我会尽力解释尽我所能..jquery - 简单的数组,如果它不在那里已经推入项目,删除项目,如果它存在

$(document).ready(function(){ 
    //so I start with an empty array 
    var filters []; 
    //when a link is clicked I want to add it to the array.. 
    $('li a', context).click(function(e){ 
     //so I get the value held in the data-event attribute of the clicked item example: "john" 
     newFilter = $(this).attr('data-event'); 
     //this is where I get stuck, I want to test to see if the string I now have 
     //in 'newFilter' is in the array already or not.. if it is in the array I 
     //want to remove it, but if it doesnt exist in the array i want to add it.. 
     if(jQuery.inArray(newFilter, filters){ 
      //add to array 
     } else { 
      //remove from array 
     }; 
    e.preventDefault(); 
    }); 
}); 
+3

你可以尝试'indexof'你的字符串对数组?如果它返回-1,那么'push',如果它大于-1,那么''pop' – MilkyWayJoe 2012-03-07 15:40:29

回答

41

$.inArray()如果找到,则返回该项目的索引,-1否则(就像indexOf()支持)。因此,你可以写这样的:

var found = jQuery.inArray(newFilter, filters); 
if (found >= 0) { 
    // Element was found, remove it. 
    filters.splice(found, 1); 
} else { 
    // Element was not found, add it. 
    filters.push(newFilter); 
} 
6

我可能是错的,但我相信这是因为使用基本的JavaScript一样简单:[.push.splice]

if($.inArray(newFilter, filters)<0) { 
    //add to array 
    filters.push(newFilter); // <- basic JS see Array.push 
} 
else { 
    //remove from array 
    filters.splice($.inArray(newFilter, filters),1); // <- basic JS see Array.splice 
}; 

当然如果你真的想简化它,你可以删除一些行并将其缩减为内联编码。

0 > $.inArray(newFilter,filters) ? filters.push(newFilter) : filters.splice($.inArray(newFilter,filters),1); 

绝对纯JS:

var i; (i=filters.indexOf(newFilter))<0?filters.push(newFilter):filters.splice(i,1); 

拆毁了

var i; // Basic variable to be used if index of item exist 
// The following is simply an opening to an inline if statement. 
// It's wrapped in() because we want `i` to equal the index of the item, if found, not what's to follow the `?`. 
// So this says "If i = an index value less than 0". 
(i=filters.indexOf(newFilter)) < 0 ? 
    // If it was not found, the index will be -1, thus push new item onto array 
    filters.push(newFilter) : 
     // If found, i will be the index of the item found, so we can now use it to simply splice that item from the array. 
     filters.splice(i,1); 
+4

'$ .inArray()'是一个jQuery函数。这不是“基本的JavaScript”... – aendrew 2013-09-20 11:55:10

+0

@aendrew我指的是wuts iinside的方法 – SpYk3HH 2013-09-20 18:15:38

0

另一种方式,我发现:

删除:

filters = jQuery.grep(filters, function(value) { 
    return value != newFilter; 
}); 

地址:

filters.push(newFilter) 
1

除非你有特殊原因对于使用数组,我会建议使用一个对象。

$(document).ready(function(){ 
    //so I start with an empty array 
    var filters {}; 
    //when a link is clicked I want to add it to the array.. 
    $('li a', context).click(function(e){ 
     //so I get the value held in the data-event attribute of the clicked item example: "john" 
     newFilter = $(this).attr('data-event'); 
     //this is where I get stuck, I want to test to see if the string I now have 
     //in 'newFilter' is in the array already or not.. if it is in the array I 
     //want to remove it, but if it doesnt exist in the array i want to add it.. 
     if (filters.hasOwnProperty(newFilter)) { 
      // remove from object 
      delete filters[newFilter]; 
     } else { 
      //add to object 
      filters[newFilter] = 'FOO'; // some sentinel since we don't care about value 
     }; 
    e.preventDefault(); 
    }); 
}); 
+0

嘿jbabey ..有点新手问题如何使用console.log查看对象中的内容?目前我得到 - [object Object] – Iamsamstimpson 2012-03-07 16:07:32

+1

console.log(myObject.propName)或console.log(myObject ['propName']) – jbabey 2012-03-07 16:09:02

0

这样的事情?

var filters = []; 
// ... 
var newFilter = '...'; 
if(-1 !== (idx = jQuery.inArray(newFilter, filters))) { 
    // remove 
    filters.splice(idx, 1); 
} else { 
    // add 
    filters.push(newFilter); 
} 
1

可以使用lodash功能“异或”:

_.xor([2, 1], [2, 3]); 
// => [1, 3] 

如果你没有一个数组作为第二个参数,你可以的simpy住变量到一个数组

var variableToInsertOrRemove = 2; 
_.xor([2, 1], [variableToInsertOrRemove]); 
// => [1] 
_.xor([1, 3], [variableToInsertOrRemove]); 
// => [1, 2, 3] 

这里的文档:https://lodash.com/docs/4.16.4#xor

相关问题