Замена значений ключа в объекте с массивом для цикла JavaScript - PullRequest
0 голосов
/ 07 декабря 2018

Я пытаюсь изменить значения в ключе "notes" на значения в массиве переменных масштаба.Таким образом, после запуска функции числа в «заметках» теперь являются соответствующими буквами из шкалы переменных.например: первые значения ключа "notes" в var chordArrays будут читать [d, f, a] вместо [1,3,5] и т. д.

  var chordArrays = [    
    {
    "chord type": "minor",
    "inversion": "root",
    "chords": [
      {
         "scale degree": "II",
         "notes": [1,3,5]
      },
      {
        "scale degree": "III",
        "notes": [2,4,6]
      },
      {
        "scale degree": "VI",
        "notes": [5,0,2]
      },
    ]
  },
  {
    "chord type": "minor",
    "inversion": "first",
    "chords": [
      {
        "scale degree": "II",
        "notes": [3,5,1]
      },
      {
        "scale degree": "III",
        "notes": [4,6,2]
      },
      {
        "scale degree": "VI",
        "notes": [0,2,5]
      },
    ]
  }]

 var scale = [c,d,e,f,g,a,b]



function getMainChordsOfKey(scale,chordArrays) {
  for (var i = 0; i < chordArrays.length; i++) {
    for (var j = 0; j < chordArrays[i].chords.length; j++) {
      for (var k = 0; k < chordArrays[i].chords[j].notes.length; k++) {
        for (var z = 0; z < scale.length; z++) {
          if (chordArrays[i].chords[j].notes[k] === z) {
              replace k with z
          }
        }
      }
    }
  }
};

Ответы [ 3 ]

0 голосов
/ 07 декабря 2018

Посмотрите, ищите ли вы это

var chordArrays = [
  {
    'chord type': 'minor',
    inversion: 'root',
    chords: [
      {
        'scale degree': 'II',
        notes: [1, 3, 5]
      },
      {
        'scale degree': 'III',
        notes: [2, 4, 6]
      },
      {
        'scale degree': 'VI',
        notes: [5, 0, 2]
      }
    ]
  },
  {
    'chord type': 'minor',
    inversion: 'first',
    chords: [
      {
        'scale degree': 'II',
        notes: [3, 5, 1]
      },
      {
        'scale degree': 'III',
        notes: [4, 6, 2]
      },
      {
        'scale degree': 'VI',
        notes: [0, 2, 5]
      }
    ]
  }
];

var scale = ['c', 'd', 'e', 'f', 'g', 'a', 'b'];

chordArrays.forEach(o => {
  o.chords.forEach(n => {
    n.notes = n.notes.map(i => (i = scale[i]));
  });
});

console.log(chordArrays);

// sample on the DOM
document.querySelector('pre').innerHTML = chordArrays[0].chords[0].notes;

вот ссылка на гист

0 голосов
/ 07 декабря 2018

Это неизменное решение (не изменяет исходный объект)

chordArrays.map(chord => ({
  ...chord,
  chords: chord.chords.map(note => ({
    ...note,
    notes: note.notes.map(note => scale[note])
  }))
}))

->

[{
"chord type": "minor",
"inversion": "root",
"chords": [
  {
    "scale degree": "II",
    "notes": [
      "d",
      "f",
      "a"
    ]
  }, ...]
0 голосов
/ 07 декабря 2018

Ваши буквы должны быть разделены кавычками, иначе JS увидит их не как строки, а как имена переменных.

Вот как вы можете это сделать:

const chordArrays = [{ "chord type": "minor", "inversion": "root", "chords": [ { "scale degree": "II", "notes": [1,3,5] }, { "scale degree": "III", "notes": [2,4,6] }, { "scale degree": "VI", "notes": [5,0,2] }, ] }, { "chord type": "minor", "inversion": "first", "chords": [ { "scale degree": "II", "notes": [3,5,1] }, { "scale degree": "III", "notes": [4,6,2] }, { "scale degree": "VI", "notes": [0,2,5] }, ] }]

const scale = "cdefgab"; // Can be array or string. But use quotes.
 
for (const type of chordArrays) {
    for (const chord of type.chords) {
        chord.notes = chord.notes.map(note => scale[note]);
    }
}
 
console.log(chordArrays);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...