首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数组与对象属性匹配

将数组与对象属性匹配
EN

Stack Overflow用户
提问于 2021-06-03 06:42:43
回答 4查看 54关注 0票数 0

我有一个对象,其属性如下所示:

代码语言:javascript
复制
{
  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函数来实现这一点,如下面的代码片段所示:

代码语言:javascript
复制
  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 ] }

非常感谢

EN

回答 4

Stack Overflow用户

发布于 2021-06-03 07:02:11

这将返回具有您正在查找的模式的所有和弦。它只是将数组展平,并查找indexOf来组合所有匹配项。可以将其作为严格(精确匹配)或包含(所有部分匹配)运行

代码语言:javascript
复制
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);

票数 2
EN

Stack Overflow用户

发布于 2021-06-03 07:00:26

你是说像这样的东西吗?编辑为使用reduce以获得所需的回报

代码语言:javascript
复制
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))

票数 1
EN

Stack Overflow用户

发布于 2021-06-03 07:04:13

使用Array#every()并匹配长度。不过,你想要的结果并不是很清楚,所以可能需要根据你的期望进行修改

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67813384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档