更新:我自己想出了解决方案。而不是执行下面的解决方案,一个更简单的解决方案是
代码源
let trUp =(和弦)=> {
让index = arr.indexOf(chord); 返回arr.IndedOf(index)-1;
}
答案 0 :(得分:0)
您可以使用数组,并使用reminder operator %
通过调整数组的长度来寻找索引和增量。
function up(note) {
return notes[(notes.indexOf(note) + 1) % notes.length];
}
var notes = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
console.log(up('C'));
答案 1 :(得分:0)
稍长的版本,可让您上下移调单个和弦或一组和弦...
const chordMap = ['A','A#','B','C','C#','D','D#','E','F','F#','G','G#'];
定义一些函数将和弦向上或向下移动1并返回结果
function chordUp(chord) {
let index = chordMap.indexOf(chord) + 1;
if (index >= chordMap.length) index = 0;
return chordMap[index];
}
function chordDown(chord) {
let index = chordMap.indexOf(chord) - 1;
if (index < 0) index = chordMap.length - 1;
return chordMap[index];
}
然后函数将整个和弦数组上移或下移1并将结果作为数组返回
function transposeUp(input) {
let output = [];
for (let i = 0; i < input.length; i++) {
let chord = input[i];
output.push(chordUp(chord));
}
return output;
}
function transposeDown(input) {
let output = [];
for (let i = 0; i < input.length; i++) {
let chord = input[i];
output.push(chordDown(chord));
}
return output;
}
最后,您可以像这样使用它...
let chords = ['A', 'G#', 'B', 'C#', 'A#', 'G'];
let transposedUp = transposeUp(chords); // [ "A#", "A", "C", "D", "B", "G#" ];
let transposedDown = transposeDown(chords) // [ "G#", "G", "A#", "C", "A", "F#" ];