javascript - 如何在 JavaScript 中为对象数组创建构造函数?
问题描述
我试图在运行时创建一个对象来表示谷歌图表的垂直轴刻度。
给定一组 MIDI(https://newt.phys.unsw.edu.au/jw/notes.html)音符编号 nn,其中范围由用户定义,我需要将其转换为对象数组形式 {v: nn, f: notes[nn]}
(notes 是现有的文本名称数组,例如 nn=60 的“C4”)。
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]}
]
ETC
我认为一种方法是将 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#'];
如果它在末尾(so 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; }
推荐阅读
- mysql - 如何找到我与其他用户的距离?
- powershell - 从 Active Directory 为用户读取组时,“GetAuthorizationGroups”引发异常
- javascript - 如何只用 js 切换一次类
- javascript - 在 Java Nashorn 中使用 JSON.parse
- laravel - Laravel Eleqent 如何在 Postgresql 中使用 DATE_FORMAT
- python-3.x - 解压 .zip 文件并使用 python 和 boto 3 传输到 s3 存储桶
- python - 如何在不关闭套接字的情况下发送和接收文件
- azure - 在 azure 中获取公共 IP 的详细信息
- python-3.x - 我可以在系统中没有 python 3.6 的情况下在 ubuntu 18.04 中安装 python 3.7 吗?
- javascript - 我需要忽略选择的文本并只使用数值,我使用的是 vue