简体   繁体   English

如何使用.includes对数组进行.filter过滤?

[英]How to .filter an array against an array with .includes?

I've looked at a number of other posts and can't resolve my case. 我看过许多其他帖子,但无法解决我的案件。

I am working on a music/chord playing program and I want to filter out all chords (aka objects) that include one or more notes (numbers in my program) that are out of key. 我正在开发一个音乐/和弦演奏程序,我想过滤掉所有包含一个或多个音符(程序中的数字)超出关键范围的和弦(又名对象)。

I have an array named chordLibrary filled with objects (eg, {notesInChord: [5, 8], chordName: 'Major'} ). 我有一个名为chordLibrary的数组, chordLibrary充满了对象(例如{notesInChord: [5, 8], chordName: 'Major'} )。 I have another array with numbers ( outOfKeyNotes = [11, 12]; ). 我还有另一个带有数字的数组( outOfKeyNotes = [11, 12]; )。 I want to .filter and return only the objects in chordLibrary that do not include the numbers in outOfKeyNotes in the element notesInChord . 我想过滤并仅返回chordLibrary中不包含元素notesInChordoutOfKeyNotes中的数字的notesInChord

For example: 例如:

 // THIS is the original array: const chordLibrary = [ { notesInChord: [5, 8], chordName: 'Major' }, { notesInChord: [5, 8, 11], chordName: 'Dominant 7th' }, { notesInChord: [4, 8, 12], chordName: 'Minor Major 7th' } ]; // THIS is what I hope to end up with after filtering for the array [11,12]: let chordsInKey = [ { notesInChord: [5, 8], chordName: 'Major' }, ]; 

Here is my program which is currently not working. 这是我当前无法正常运行的程序。 it simply returns the whole original array. 它只是返回整个原始数组。

 const chordLibrary = [ { notesInChord: [5, 8], chordName: 'Major' }, { notesInChord: [5, 8, 11], chordName: 'Dominant 7th' }, { notesInChord: [4, 8, 12], chordName: 'Minor Major 7th' } ]; let outOfKeyNotes = [11, 12]; console.log(chordLibrary.length); let chordsInKey = chordLibrary.filter(function(item) { return !outOfKeyNotes.includes(item.notesInChord); }); console.log(chordsInKey); console.log(chordsInKey.length); 

If I change the program so that chordLibrary is simply an array of number values instead of an array of objects, it works fine. 如果我更改程序,使chordLibrary只是数字值的数组而不是对象的数组,那么它可以正常工作。 It just doesn't suit my needs. 只是不符合我的需要。 Here is a working example of that: 这是一个可行的示例:

 let chordLibrary = [1,2,3,11,12]; let outOfKeyNotes = [11, 12]; console.log(chordLibrary.length); let chordsInKey = chordLibrary.filter(function(item) { return !outOfKeyNotes.includes(item); }); console.log(chordsInKey); console.log(chordsInKey.length); 

What am I missing? 我想念什么? Thanks 谢谢

You can use Array#some() to check if any of the notesInChord values exist in outOfKeyNotes 您可以使用Array#some()检查notesInChordoutOfKeyNotes存在任何notesInChord

 const chordLibrary = [ { notesInChord: [5, 8], chordName: 'Major' }, { notesInChord: [5, 8, 11], chordName: 'Dominant 7th' }, { notesInChord: [4, 8, 12], chordName: 'Minor Major 7th' } ]; let outOfKeyNotes = [11, 12]; let chordsInKey = chordLibrary.filter(function(item) { return !item.notesInChord.some(function(v){ return outOfKeyNotes.includes(v); }); }); console.log(chordsInKey); 

You're trying to use includes to see if an array of values includes another array of values. 您尝试使用includes来查看值数组是否包含另一个值数组。

Instead, you can use Array.every in your filtering mechanism, which makes sure every element in an array passes a specific test. 相反,您可以在过滤机制中使用Array.every ,以确保数组中的每个元素都通过特定的测试。

I would change 我会改变

return !outOfKeyNotes.includes(item.notesInChord);

to

return item.notesInChord.every(note => !outOfKeyNotes.includes(note));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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