我有一个对象,其属性如下所示:
{
maj6: { chromatic: [ 0, 4, 7, 9 ] },
min6: { chromatic: [ 0, 3, 7, 9 ] },
maj7: { chromatic: [ 0, 4, 7, 11 ] },
min7: { chromatic: [ 0, 3, 7, 10 ] },
minmaj7: { chromatic: [ 0, 3, 7, 11 ] },
dom7: { chromatic: [ 0, 4, 7, 10 ] },
maj9: { chromatic: [ 0, 4, 7, 11, 14 ] },
dom9: { chromatic: [ 0, 4, 7, 10, 14 ] },
maj: { chromatic: [ 0, 4, 7 ] },
min: { chromatic: [ 0, 3, 7 ] },
aug: { chromatic: [ 0, 4, 8 ] },
dim: { chromatic: [ 0, 3, 6 ] }
}我正在计算一个数组,例如0,4,7,我试图将其与上述对象属性之一相匹配。我尝试使用find函数来实现这一点,如下面的代码片段所示:
matchParsedToChord() {
const result = this.blocks //returns larger payload
.map(block => block.chromatic) // returns array [0,4,7,4,7,7,7,4]
.filter((v,i,arr) => arr.indexOf(v) === i) //removes redundancies [0,4,7]
.find(v => { //attempts to
v === Object.keys(chordDefinitions).map(key => chordDefinitions[key].chromatic)
})
console.log(result)
}我的问题是Object.keys逻辑返回一个完整的数组,而我喜欢逐个元素进行比较。
我想返回与数组关联的对象属性,例如:
maj: { chromatic: [ 0, 4, 7 ] }
非常感谢
发布于 2021-06-03 07:02:11
这将返回具有您正在查找的模式的所有和弦。它只是将数组展平,并查找indexOf来组合所有匹配项。可以将其作为严格(精确匹配)或包含(所有部分匹配)运行
const a = {
maj6: { chromatic: [ 0, 4, 7, 9 ] },
min6: { chromatic: [ 0, 3, 7, 9 ] },
maj7: { chromatic: [ 0, 4, 7, 11 ] },
min7: { chromatic: [ 0, 3, 7, 10 ] },
minmaj7: { chromatic: [ 0, 3, 7, 11 ] },
dom7: { chromatic: [ 0, 4, 7, 10 ] },
maj9: { chromatic: [ 0, 4, 7, 11, 14 ] },
dom9: { chromatic: [ 0, 4, 7, 10, 14 ] },
maj: { chromatic: [ 0, 4, 7 ] },
min: { chromatic: [ 0, 3, 7 ] },
aug: { chromatic: [ 0, 4, 8 ] },
dim: { chromatic: [ 0, 3, 6 ] }
}
function getPattern(pattern, strict) {
let b = []
for (const [key, value] of Object.entries(a)) {
let set = Object.values(value).flat().join(",") ;
if ((!strict && set.indexOf(pattern) !== -1) || (strict && set == pattern)) b.push({
[key]: value
})
}
if (strict && b.length>0) b = b[0];
return b;
}
// Contains (partial matches)
let matches = getPattern("0,4,7", false);
console.log('Contains', matches);
// strict
matches = getPattern("0,4,7", true);
console.log('Strict', matches);
发布于 2021-06-03 07:00:26
你是说像这样的东西吗?编辑为使用reduce以获得所需的回报
const a = {
maj6: { chromatic: [ 0, 4, 7, 9 ] },
min6: { chromatic: [ 0, 3, 7, 9 ] },
maj7: { chromatic: [ 0, 4, 7, 11 ] },
min7: { chromatic: [ 0, 3, 7, 10 ] },
minmaj7: { chromatic: [ 0, 3, 7, 11 ] },
dom7: { chromatic: [ 0, 4, 7, 10 ] },
maj9: { chromatic: [ 0, 4, 7, 11, 14 ] },
dom9: { chromatic: [ 0, 4, 7, 10, 14 ] },
maj: { chromatic: [ 0, 4, 7 ] },
min: { chromatic: [ 0, 3, 7 ] },
aug: { chromatic: [ 0, 4, 8 ] },
dim: { chromatic: [ 0, 3, 6 ] }
}
const find = (obj, match = [0,4,7]) => {
return Object.keys(a)
.reduce((acc, k) => {
// If you want more loose match, just switch search direction
// like: match.every(m => obj[k].chromatic.includes(m)
if(obj[k].chromatic.every(m => match.includes(m))) {
acc[k] = obj[k];
}
return acc;
},
{})
}
console.log(find(a))
发布于 2021-06-03 07:04:13
使用Array#every()并匹配长度。不过,你想要的结果并不是很清楚,所以可能需要根据你的期望进行修改
const data ={
maj6: { chromatic: [ 0, 4, 7, 9 ] },
min6: { chromatic: [ 0, 3, 7, 9 ] },
maj7: { chromatic: [ 0, 4, 7, 11 ] },
min7: { chromatic: [ 0, 3, 7, 10 ] },
minmaj7: { chromatic: [ 0, 3, 7, 11 ] },
dom7: { chromatic: [ 0, 4, 7, 10 ] },
maj9: { chromatic: [ 0, 4, 7, 11, 14 ] },
dom9: { chromatic: [ 0, 4, 7, 10, 14 ] },
maj: { chromatic: [ 0, 4, 7 ] },
min: { chromatic: [ 0, 3, 7 ] },
aug: { chromatic: [ 0, 4, 8 ] },
dim: { chromatic: [ 0, 3, 6 ] }
}
const arr = [0,4,7];
const entries = Object.entries(data).map(([k,v])=> [k, [...new Set(v.chromatic)]]);
const match = entries.find(([k,v])=> v.length === arr.length && arr.every(n => v.includes(n)))
console.log(match)
https://stackoverflow.com/questions/67813384
复制相似问题