简体   繁体   English

如何从Javascript中的数组中提取多个元素?

[英]How to pull multiple elements from an array in Javascript?

So I am working on a school project to create a "piano" that will play notes that start at a certain time and return an output that looks like this:因此,我正在开展一个学校项目来创建一个“钢琴”,该项目将播放在特定时间开始的音符并返回如下所示的输出:

    Outputs:
    Play A
    Play B
    Wait 1.5 seconds
    Play C
    Wait 1.5 seconds
    Release A
    Release B
    Release C

So on and so forth.等等等等。 I am able to sort the start times, but I can't figure out how to write something that will translate to the output I need.我能够对开始时间进行排序,但我不知道如何编写一些可以转换为我需要的输出的东西。 I'm not sure how to pull the note when the format is note: "A".当格式为 note:"A" 时,我不确定如何拉出笔记。 I am hoping to write something that will pull the notes that start at 0,1,2,etc in order, tell them to play for a certain amount of time and then release.我希望能写出一些东西,把从 0、1、2 等开始的音符按顺序拉出来,告诉他们演奏一定的时间然后释放。 I'm not really sure how else to ask this.我真的不知道还能怎么问这个。

    let piano=[
      {note: 'Ab', starts_at: 0},
      {note: 'A', starts_at: 5},
      {note: 'A#', starts_at: 10},
      {note: 'Bb', starts_at: 7},
      {note: 'B', starts_at: 4},
      {note: 'C', starts_at: 8},
      {note: 'C#', starts_at: 13},
      {note: 'Db', starts_at: 2},
      {note: 'D', starts_at: 0},
      {note: 'D#', starts_at: 5},
      {note: 'Eb', starts_at: 1},
      {note: 'E', starts_at: 11},
      {note: 'F', starts_at: 3},
      {note: 'F#', starts_at: 2},
      {note: 'Gb', starts_at: 9},
      {note: 'G', starts_at: 10},
      {note: 'G#', starts_at: 1}
    ];


    let my_song=piano.sort((elem_1, elem_2,) =>{
      if (elem_1.starts_at == elem_2.starts_at){
        return 0;
      } else if (elem_1.starts_at >= elem_2.starts_at){
        return 1;
      }
     return -1;
    });
    console.log(my_song)

I think you can put a reduce after you sort the array to build the string value.我认为您可以在对数组进行排序以构建字符串值后放置一个 reduce。

const str = piano.reduce((acc, curr, i, arr)=>{
  const prev = arr[i - 1];
  if(prev) {
    const diff = curr.starts_at - prev.starts_at;
    acc += `\nWait ${diff} seconds`;
  }
  acc += `\nPlay ${curr.note}`;
}, '');

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

相关问题 如何在JavaScript中使用过滤器从多维数组中删除多个元素? - How to remove multiple elements from multidimensional array with filter in JavaScript? 从数组3中乘3随机提取元素 - Pull elements randomly from an array 3 by 3 变量以从JavaScript中的数组中提取多个值(音乐理论游戏) - variable to pull multiple values from array in javascript (music theory game) 从 Javascript/jQuery 中的数组中删除多个元素 - Remove multiple elements from array in Javascript/jQuery 如果使用JavaScript,如何检查数组中的多个元素? - How to check multiple elements in array with one if with JavaScript? 如何从Linkedin Javascript API数组中提取phoneNumber - How do I pull phoneNumber from Linkedin Javascript API array 如何从 javascript 中没有重复的数组中提取随机字符串? - how to pull random strings from an array with no repeats in javascript? 从另一个数组中过滤 object 的数组,在 javascript 中有多个元素 - filter array of object from another array with multiple elements in javascript 如何在NodeJS中从一个数组中提取多个项目而不重复? - How to pull multiple items from one array without repeats in NodeJS? 如何从数组中拉出元素,其中元素的字符串长度>较大? - How to $pull elements from an array, $where elements' string length > a large number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM