Consider the following JS array:
let text = [
{
line: [{
words: [{
word: [
{ letter: 'H' },
{ letter: 'i' },
{ letter: ' ' },
],
},
{
word: [
{ letter: 'J' },
{ letter: 'i' },
{ letter: 'm' },
{ letter: 'n' },
],
}],
}]
},
{
line: [{
words: [{
word: [
{ letter: 'H' },
{ letter: 'i' },
{ letter: ' ' },
],
},
{
word: [
{ letter: 'J' },
{ letter: 'o' },
{ letter: 'n' },
],
}],
}]
}
]
Is there a fast (and simple) way to access the nth letter?
For example :
- if n=0, get ‘H’
- if n=1, get ‘i’
- …
- if n=11, get ‘o’
Here is a JSFiddle: https://jsfiddle.net/Imabot/6usdej75/
2
Answers
I solved it with
flat
andflatMap
:I think a good option is to use any loop to break it at the index you need: