简体   繁体   English

如何为 JavaScript 中的对象数组创建构造函数?

[英]How can I create a constructor for an array of objects in JavaScript?

I'm trying to create an object, at run time, to represent vertical axis ticks for a Google chart.我试图在运行时创建一个 object 来表示 Google 图表的垂直轴刻度。

Given an array of MIDI ( https://newt.phys.unsw.edu.au/jw/notes.html ) note numbers nn, where the range is defined by the user, I need to transform this to an array of objects of the form {v: nn, f: notes[nn]}给定一组 MIDI ( https://newt.phys.unsw.edu.au/jw/notes.html ) 音符编号 nn,其中范围由用户定义,我需要将其转换为对象数组形式 {v: nn, f: notes[nn]}

(notes is an existing array of text names, such as "C4" for nn=60). (注释是现有的文本名称数组,例如 nn=60 的“C4”)。

notes = ['A0', 'A#0', 'B0', 'C1', ... , 'C8'] 

and corresponding nn values would be:相应的 nn 值将是:

nnArr = [21, 22, 23, 24, ... , 108] 

The end results would be something like this:最终结果将是这样的:

vAxisTicks= [
  {v: 56, f: notes[56-21]}, {v: 57, f: notes[57-21}, {v: 58, f: notes[58-21]}
]

etc ETC

I think an approach is to use Array with.map but I can't see how to use that to get the keys in the objects, despite several forays into the literature!我认为一种方法是使用 Array with.map 但我看不出如何使用它来获取对象中的键,尽管对文献进行了多次尝试!

If I understood correctly you want to generate like this?:如果我理解正确你想生成这样的?:

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

Output is: Output 是:

[
  {
    "v": "21",
    "f": "A0"
  },
  {
    "v": "22",
    "f": "A#0"
  },
  {
    "v": "23",
    "f": "B0"
  },
  {
    "v": "24",
    "f": "C1"
  },
  ...
]

Important are these lines:这些行很重要:

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

that means loop from i = 21 to i = 108 - I got this from: https://newt.phys.unsw.edu.au/jw/notes.html .这意味着从i = 21i = 108的循环 - 我从https://newt.phys.unsw.edu.au/jw/notes.html得到这个。

Every iteration of the loop add on one to i and add on one to y unless y is already equal to the last index of the letters array:循环的每次迭代都对i加一,对y加一,除非y已经等于字母数组的最后一个索引:

const letters = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];

if it is at the end (so y == 11 ), then reset back to zero: y = 0 .如果它在末尾(所以y == 11 ),则重置回零: y = 0

Then you have created a notes object like:然后你创建了一个笔记 object 像:

{21: 'A0', 22: 'A#0', 23: 'B0', 24: 'C1', ... , 108: 'C8'}

You can then loop over the entries with Object.entries(notes) .然后,您可以使用Object.entries(notes)条目。

This gives you an array like this:这给你一个这样的数组:

[[21, 'A0'], [22, 'A#0'], [23, 'B0'], [24, 'C1'], ... , [108, 'C8']]

Which you can loop over with .map() to create your desired output.您可以使用.map()循环创建所需的 output。

NOTE: we could have just created this up-front in the for-loop but it seemed like you already had this notes object.注意:我们本可以在 for 循环中预先创建这个,但看起来你已经有了这个注释 object。

Alternatively:或者:

If you want to take an array as input like [56, 57, 58]如果您想将数组作为输入,例如[56, 57, 58]

Then you can do this:然后你可以这样做:

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

output: output:

[
  {
    "v": 56,
    "f": "G#3"
  },
  {
    "v": 57,
    "f": "A3"
  },
  {
    "v": 58,
    "f": "A#3"
  }
]

Alternatively:或者:

If I assume like I just edited your question, that you have an array for notes like:如果我假设我刚刚编辑了你的问题,那么你有一个用于注释的数组,例如:

notes = ['A0', 'A#0', 'B0', 'C1', ... , 'C8'] 

and corresponding nn values would be:相应的 nn 值将是:

nnArr = [21, 22, 23, 24, ... , 108] 

Then we can solve it very similarly:然后我们可以非常相似地解决它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM