skip to Main Content

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


  1. I solved it with flat and flatMap:

    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' },
            ],
          }],
        }]
      }
    ];
    
    const flat = text.map(({ line }) => line.map(({ words }) => words.map(({ word }) => word)).flatMap(item => item.flat())).flat();
    function getIndex(array, index) {
      return array[index];
    }
    console.log(getIndex(flat, 0));
    console.log(getIndex(flat, 1));
    console.log(getIndex(flat, 11));
    Login or Signup to reply.
  2. I think a good option is to use any loop to break it at the index you need:

    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' },
          ],
        }],
      }]
    }
    ];
    
    console.log( findLetter(text, 0) );
    console.log( findLetter(text, 1) );
    console.log( findLetter(text, 11) );
    
    function findLetter(text = [], index = 0) {
      let i = 0;
      for (let {line} of text) {
        for (let {words} of line) {
          for (let {word} of words) {
            if (word.length + i <= index) {
              i += word.length;
            } else {
              return word[index - i].letter;
            }
          }
        }
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search