如何为 JavaScript 中的对象数组创建构造函数?
How can I create a constructor for an array of objects in JavaScript?
我正在尝试在 运行 时间创建一个对象,以表示 Google 图表的垂直轴刻度。
给定一组 MIDI (https://newt.phys.unsw.edu.au/jw/notes.html) 音符编号 nn,其中范围由用户定义,我需要将其转换为 {v: nn, f:笔记[nn]}
(notes是已有的文字名称数组,如"C4"表示nn=60)。
notes = ['A0', 'A#0', 'B0', 'C1', ... , 'C8']
相应的 nn 值将是:
nnArr = [21, 22, 23, 24, ... , 108]
最终结果会是这样的:
vAxisTicks= [
{v: 56, f: notes[56-21]}, {v: 57, f: notes[57-21}, {v: 58, f: notes[58-21]}
]
等等
我认为一种方法是将 Array 与 .map 一起使用,但我看不出如何使用它来获取对象中的键,尽管对文献进行了多次尝试!
如果我没理解错你想这样生成?:
const notes = {};
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
let num = 0;
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num ++;
notes[i] = letters[y] + num;
}
//console.log(notes)
const output = Object.entries(notes).map(([key,val]) => {
return {v: key, f: val}
});
console.log(output)
//It could be generated in one loop with:
/*
const notesArr = [];
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num ++;
notesArr.push({v: i, f: letters[y] + num});
}
console.log(notesArr)
*/
.as-console-wrapper { max-height: 100% !important; top: 0; }
输出为:
[
{
"v": "21",
"f": "A0"
},
{
"v": "22",
"f": "A#0"
},
{
"v": "23",
"f": "B0"
},
{
"v": "24",
"f": "C1"
},
...
]
重要的是这些行:
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num ++;
notes[i] = letters[y] + num;
}
这意味着从 i = 21
循环到 i = 108
- 我从以下位置得到这个:https://newt.phys.unsw.edu.au/jw/notes.html.
循环的每次迭代都会在 i
上加一,在 y
上加一,除非 y
已经等于字母数组的最后一个索引:
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
如果在最后(所以y == 11
),则重置回零:y = 0
.
然后您创建了一个注释对象,如下所示:
{21: 'A0', 22: 'A#0', 23: 'B0', 24: 'C1', ... , 108: 'C8'}
然后您可以使用 Object.entries(notes)
遍历条目。
这给你一个像这样的数组:
[[21, 'A0'], [22, 'A#0'], [23, 'B0'], [24, 'C1'], ... , [108, 'C8']]
您可以使用 .map()
循环创建您想要的输出。
注意: 我们本可以在 for 循环中预先创建它,但看起来您已经有了这个 notes 对象。
或者:
如果你想像 [56, 57, 58]
这样的数组作为输入
那么你可以这样做:
const notes = {};
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
let num = 0;
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num++;
notes[i] = letters[y] + num;
}
//console.log(notes)
const inputNums = [56, 57, 58];
const output = inputNums.map(item => {
return {v: item, f: notes[item]}
});
console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }
输出:
[
{
"v": 56,
"f": "G#3"
},
{
"v": 57,
"f": "A3"
},
{
"v": 58,
"f": "A#3"
}
]
或者:
如果我假设我刚刚编辑了您的问题,那么您有一个注释数组,例如:
notes = ['A0', 'A#0', 'B0', 'C1', ... , 'C8']
相应的 nn 值将是:
nnArr = [21, 22, 23, 24, ... , 108]
那么我们可以很类似的解决:
const notes = [];
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
let num = 0;
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num++;
notes.push(letters[y] + num);
}
//console.log(notes)
const input = [56, 57, 58]
const output = input.map(i => {
return {v: i, f: notes[i - 21]}
});
console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }
我正在尝试在 运行 时间创建一个对象,以表示 Google 图表的垂直轴刻度。
给定一组 MIDI (https://newt.phys.unsw.edu.au/jw/notes.html) 音符编号 nn,其中范围由用户定义,我需要将其转换为 {v: nn, f:笔记[nn]}
(notes是已有的文字名称数组,如"C4"表示nn=60)。
notes = ['A0', 'A#0', 'B0', 'C1', ... , 'C8']
相应的 nn 值将是:
nnArr = [21, 22, 23, 24, ... , 108]
最终结果会是这样的:
vAxisTicks= [
{v: 56, f: notes[56-21]}, {v: 57, f: notes[57-21}, {v: 58, f: notes[58-21]}
]
等等
我认为一种方法是将 Array 与 .map 一起使用,但我看不出如何使用它来获取对象中的键,尽管对文献进行了多次尝试!
如果我没理解错你想这样生成?:
const notes = {};
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
let num = 0;
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num ++;
notes[i] = letters[y] + num;
}
//console.log(notes)
const output = Object.entries(notes).map(([key,val]) => {
return {v: key, f: val}
});
console.log(output)
//It could be generated in one loop with:
/*
const notesArr = [];
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num ++;
notesArr.push({v: i, f: letters[y] + num});
}
console.log(notesArr)
*/
.as-console-wrapper { max-height: 100% !important; top: 0; }
输出为:
[
{
"v": "21",
"f": "A0"
},
{
"v": "22",
"f": "A#0"
},
{
"v": "23",
"f": "B0"
},
{
"v": "24",
"f": "C1"
},
...
]
重要的是这些行:
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num ++;
notes[i] = letters[y] + num;
}
这意味着从 i = 21
循环到 i = 108
- 我从以下位置得到这个:https://newt.phys.unsw.edu.au/jw/notes.html.
循环的每次迭代都会在 i
上加一,在 y
上加一,除非 y
已经等于字母数组的最后一个索引:
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
如果在最后(所以y == 11
),则重置回零:y = 0
.
然后您创建了一个注释对象,如下所示:
{21: 'A0', 22: 'A#0', 23: 'B0', 24: 'C1', ... , 108: 'C8'}
然后您可以使用 Object.entries(notes)
遍历条目。
这给你一个像这样的数组:
[[21, 'A0'], [22, 'A#0'], [23, 'B0'], [24, 'C1'], ... , [108, 'C8']]
您可以使用 .map()
循环创建您想要的输出。
注意: 我们本可以在 for 循环中预先创建它,但看起来您已经有了这个 notes 对象。
或者:
如果你想像 [56, 57, 58]
那么你可以这样做:
const notes = {};
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
let num = 0;
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num++;
notes[i] = letters[y] + num;
}
//console.log(notes)
const inputNums = [56, 57, 58];
const output = inputNums.map(item => {
return {v: item, f: notes[item]}
});
console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }
输出:
[
{
"v": 56,
"f": "G#3"
},
{
"v": 57,
"f": "A3"
},
{
"v": 58,
"f": "A#3"
}
]
或者:
如果我假设我刚刚编辑了您的问题,那么您有一个注释数组,例如:
notes = ['A0', 'A#0', 'B0', 'C1', ... , 'C8']
相应的 nn 值将是:
nnArr = [21, 22, 23, 24, ... , 108]
那么我们可以很类似的解决:
const notes = [];
const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
let num = 0;
for (let i = 21, y = 0; i <= 108; i++, y >= letters.length - 1 ? y = 0 : y++){
if (letters[y] == "C") num++;
notes.push(letters[y] + num);
}
//console.log(notes)
const input = [56, 57, 58]
const output = input.map(i => {
return {v: i, f: notes[i - 21]}
});
console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }