首页 > 解决方案 > 如何在 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 一起使用,但我看不到如何使用它来获取对象中的键,尽管在文献中进行了几次尝试!

标签: javascriptarraysgoogle-visualization

解决方案


如果我理解正确,您想像这样生成?:

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; }


推荐阅读