0

I've got an array of objects of this signature:

Note {
      _payload: [Object],
      blockText: 'ti',
      measure: [Measure],
      entries: [Array],
      start: 500,
      voiceId: undefined,
      octaveModifier: 0,
      previousNote: [Note],
      firstNoteOfPreviousBlock: [Chord],
      isTied: undefined,
      currentSubgroup: undefined,
      userInput: 'do refala misoti fa',
      crescendoIndex: undefined,
      chord: [Chord],
      _duration: 250
    }

There are getter functions baked into the API which allow me to access specific object properties, such as: block.elapsedTime (which is the 'time' value), and ${note.alpha}${note.octave}, which is the ('note' value).

Ultimately, I am trying to iterate through the list of objects so that I can return objects with time and note values in this format:

[
 {time: 0, note: 'C4', velocity: 0.7 },
 {time: 250, note: 'D4', velocity: 0.7}
 {time: 250, note: 'F4', velocity: 0.7}
 {time: 250, note: 'A4', velocity: 0.7}
 {time: 500, note: 'E4', velocity: 0.7}
 {time: 500, note: 'G4', velocity: 0.7}
 {time: 500, note: 'B4', velocity: 0.7}
 {time: 750, note: 'F4', velocity: 0.7}
] 

I've tried to use map in this vein: block.entries.map(note => [block.elapsedTime, `${note.alpha}${note.octave}`])

but this returns arrays instead of objects.

Many thanks, Nakul

6
  • Is velocity a constant value (0.7)? Will you get note.alpha from previousNote' field? How is the structure of block` object? Is it like {block : elapsedTime, note:{alpha:1,octave:1}? Commented May 26, 2021 at 6:20
  • If all the objects have the same block.elapsedTime value, why are they different in the output? There is no alpha and octave properties in note Commented May 26, 2021 at 6:20
  • @RajdeepDebnath, the note class extends the block class. .alpha is an instance getter method that retrieves the data from lower down at the block level. It's payload is even richer than the one I printed. Commented May 26, 2021 at 6:27
  • @adiga, I meant to say that the payload listed above describes the signature of the object, not the array of objects. Each object in the object array has a different elapsed time value. Commented May 26, 2021 at 6:28
  • 1
    @NakulTiruviluamala so as note class extends block class, then note object will have elapsedTime property. so the returned object can be {time: note.elapsedTime, note: '${}${}'} Commented May 26, 2021 at 6:32

1 Answer 1

1

Array.prototype.map() returns an array of elments returned from callback function. In your case it is an array:

[block.elapsedTime, `${note.alpha}${note.octave}`]

You should return an object instead of array like that:

block.entries.map((note) => ({
    time: block.elapsedTime,
    note: `${note.alpha}${note.octave}`
  })
);

You can add any required field inside the object.

Sign up to request clarification or add additional context in comments.

5 Comments

But how would he get block.elapsedTime? It looks it's not being passed to the arrow function?
block variable is visible in the scope of map callback function
then block.elapsedTime will always constant for every iteration? I mean if block.elapsedTime is different for different note then the returned object will not capture that.
Yes. But in provider example it looks like a constant.
It works! Thanks again: [ { time: 0, note: 'C4' }, { time: 0, note: 'C4' }, { time: 250, note: 'D4' }, { time: 250, note: 'F4' }, { time: 250, note: 'A4' }, { time: 500, note: 'E4' }, { time: 500, note: 'G4' }, { time: 500, note: 'B4' }, { time: 750, note: 'F4' }, { time: 750, note: 'F4' } ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.