簡體   English   中英

匹配具有 object 屬性的數組

[英]Match an array with an object property

我有一個 object ,其屬性如下所示:

{
  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] 我試圖與上述 object 屬性之一匹配。 我正在嘗試使用 find function 來實現這一點,如以下代碼片段所示:

  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 邏輯返回整個數組,而我想進行逐個元素的比較。

我想返回與數組關聯的 object 屬性,例如:

maj: { chromatic: [ 0, 4, 7 ] }

非常感謝

這將返回具有您正在尋找的模式的所有和弦。 它只是將 arrays 展平並尋找 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);

你的意思是這樣的嗎? 編輯使用 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))

使用 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)

您不應該在從this.blocks獲得的數組上使用find() 那只會返回一個像3這樣的數字。

我假設您想返回與整個數組匹配的chordDefinitions條目。 因此,您應該將該數組放入一個變量中,然后chordDefinitions中搜索相等的chromatic屬性。

我已將其轉換為Set以刪除重復項並提高搜索效率。

 function matchParsedToChord() { const chord = new Set( this.blocks //returns larger payload.map(block => block.chromatic) // returns array [0,4,7,4,7,7,7,4] ); // Converting to Set removes duplicates const chordSize = thisChord.size; const result = Object.entries(chordDefinitions).find(([name, val]) => val.chromatic.length == chordSize && val.chromatic.every(v => chord.has(v))); console.log(result) }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM