繁体   English   中英

在多个条件下过滤javascript数组

[英]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#filterArray#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM