2012-03-07 42 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

相關問題