[英]filter javascript array on multiple conditions
我有一個輸入框,它過濾一個數組,只顯示那些匹配項。
let songs = [
{name: 'Let It Be', artist: 'The Beatles},
{name: 'Lady Madonna', artist: 'The Beatles },
{name: 'Mama Mia', artist: 'The Beatles}
];
在下面的示例中,它將僅按名稱進行匹配。
let value = 'name';
let q = 'Let it be' // value entered in input box;
let songFilter = songs.filter(function(song) {
return song[value].toString().toLowerCase().indexOf(q) != -1; // returns true or false
});
后來,當我進入Let It Be
在輸入框中會顯示的回報只是'Let It Be'
。 但是,我想按兩首歌曲進行過濾,因此,如果輸入“ Let It Be, Lady Madonna
我希望它返回兩首歌曲。
我采取了許多方法,但無法弄清楚如何使它正常工作。 如果這樣做更容易解決,我也可以提供lodash
。
let songFilter = songs.filter(function(song) {
['let it be', 'lady madonna'].indexOf(song[value].toString().toLowerCase()) > -1
});
您可以使用Set
鍵輸入已輸入的條目(在split
之后)。 這樣一來,您就可以在恆定時間內查看是否存在匹配項。 這里將Set
傳遞給filter
作為上下文,因此可以用以下方法引用this
:
let songs = [ {name: 'Let It Be', artist: 'The Beatles' }, {name: 'Lady Madonna', artist: 'The Beatles' }, {name: 'Mama Mia', artist: 'The Beatles' } ]; function find(byKey, valueCsv) { return songs.filter(function(song) { return this.has(song[byKey].toLowerCase()) }, new Set(valueCsv.trim().toLowerCase().split(/\\s*,\\s*/))); } songInput.addEventListener('input', function() { matches.textContent = JSON.stringify(find('name', songInput.value), null, 2); });
Type song name(s):<input id="songInput" style="width:100%"> <pre id="matches"></pre>
使用Array#filter
和Array#find
let songs = [ {name: 'Let It Be', artist: 'The Beatles'}, {name: 'Lady Madonna', artist: 'The Beatles' }, {name: 'Mama Mia', artist: 'The Beatles'} ]; let value = 'name'; let q = 'Let it be,Lady Madonna' // value entered in input box; let splitQ = q.split(',').map(sq=>sq.toLowerCase()); let songFilter = songs.filter(s=>splitQ.find(sq=>s[value].toLowerCase().indexOf(sq) > -1 ) !== undefined ); console.log(songFilter);
正如我在上面的評論中所說:
let songs = [ {name: 'Let It Be', artist: 'The Beatles'}, {name: 'Lady Madonna', artist: 'The Beatles'}, {name: 'Mama Mia', artist: 'The Beatles'} ]; let value = 'name'; let q = 'Let it be, Lady madonna'; // First: split the q value into multiple songs (and make them lowercased) let qSongs = q.split(', ').map(e => e.toLowerCase()); // Second: filter song that are in the above resultant array (qSongs) // for each song in the songs array let songFilter = songs.filter(song => // see if there is some qsong in the qSongs array such that the value of song is equal to qsong qSongs.some(qsong => song[value].toLowerCase().indexOf(qsong) != -1 ) ); console.log(songFilter);
let songs = [ { name: 'Let It Be', artist: 'The Beatles' }, { name: 'Lady Madonna', artist: 'The Beatles' }, { name: 'Mama Mia', artist: 'The Beatles' } ]; let value = 'name'; let q = 'Let it be, Lady madonna'; let songFilter = _.filter(songs, (song) => { let regex = new RegExp(q.replace(/\\s*,\\s*/g, '|'), 'gi'); return regex.test(song[value]); }); console.log(songFilter);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.