如何在JavaScript中为对象数组创建构造函数? [关闭]

问题描述 投票:1回答:1

我正在尝试在运行时创建一个对象,以表示Google图表的垂直轴刻度。

给定一个MIDI(https://newt.phys.unsw.edu.au/jw/notes.html)音符编号nn的数组,其中范围由用户定义,我需要将其转换为{v:nn,f:notes [nn]}形式的对象数组

((注释是文本名称的现有数组,例如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]}
]

[我认为一种方法是将Array与.map一起使用,但尽管有很多文献报道,但我看不到如何使用它来获取对象中的键!

javascript arrays google-visualization
1个回答
1
投票

如果我理解正确,您想生成这样的内容吗::

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 = 21i = 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()进行循环以创建所需的输出。

NOTE:我们本可以在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; }
© www.soinside.com 2019 - 2024. All rights reserved.