0

I'm parsing a json file and at this point have gathered up data from a spreadsheet. Can anyone tell me why when I set an array like this

function getfaq()
        {
          fetch(faq, settings)
              .then(res => res.json())
              .then((json) => {
                for(var i = 5;i<json.feed.entry.length;i++)
                {
                  col = json.feed.entry[i].gs$cell.col
                  faqdata[i-5] = [];
                  if(col == 1) {
                    faqdata[i-5][0] = "Question:"
                    faqdata[i-5][1] = json.feed.entry[i].gs$cell.inputValue
                  } else if(col == 2)
                  {
                    faqdata[i-5][0] = "Answer:"
                    if(json.feed.entry[i].gs$cell.inputvalue != "")
                    {
                      faqdata[i-5][1] = json.feed.entry[i].gs$cell.inputValue
                    } else {
                      faqdata[i-5][1] = "N/A"
                    }
                  } else if(col == 3)
                  {
                    faqdata[i-5][0] = "Command:"
                    faqdata[i-5][1] = json.feed.entry[i].gs$cell.inputValue
                  } else if(col == 4)
                  {
                    faqdata[i-5][0] = "Image:"
                    faqdata[i-5][1] = json.feed.entry[i].gs$cell.inputValue
                  } else if(col == 5)
                  {
                    faqdata[i-5][0] = "Reference:"
                    faqdata[i-5][1] = json.feed.entry[i].gs$cell.inputValue
                  }
                }
                for(var j = 0;j<faqdata.length;j++)
                {
                  if(faqdata[j][0].includes("Command:"))
                  {
                    faqC[j] = faqdata[j][1]
                    console.log(faqdata[j][1]);
                  }
                }
                
              })
            }

my output looks like this

    [
  <1 empty item>,  'reset',
  <2 empty items>, 'justmaxed',
  <2 empty items>, 'ascended',
  <1 empty item>,  'masteries',
  <1 empty item>,  'gliding',
  <1 empty item>,  'mounts',
  <2 empty items>, 'livingworld',
  <2 empty items>, 'gold',
  <2 empty items>, 'dungeons',
  <3 empty items>, 'fractals',
  <1 empty item>,  'raid'
]

when I just throw a log into it my output looks like

reset
justmaxed
ascended
masteries
gliding
mounts
livingworld
gold
dungeons
fractals
raid

I don't understand why it's adding in when I try to import it into an array but when I log the data it displays correctly. I have tried my best to figure it out but I do not know why its adding into the array like this...

5
  • 2
    Can you add more details, please? Commented Aug 19, 2020 at 15:14
  • 1
    Please include the initial data for those arrays (Or a small sample if it's a large array) Commented Aug 19, 2020 at 15:15
  • 1
    Because some of the items in faqdata don't satisfy your condition. Yet, when they do, you're pushing to a specific index (j) that has incremented regardless. Try faqC.push(faqdata[j][1]) instead Commented Aug 19, 2020 at 15:17
  • 1
    what happens if your IF condition isn't met? The j counter still increments, so you might end up setting array[0]. skipping array[1] and [2], then adding array[3]. Commented Aug 19, 2020 at 15:17
  • 1
    Please provide a minimal reproducible example. Commented Aug 19, 2020 at 15:17

2 Answers 2

3

One way to avoid setting nonconsecutive elements of the array by index is to instead use Array.push

instead of

faqC[j] = faqdata[j][1]

try

faqC.push(faqdata[j][1])

ALSO, in the spirit of modern functional programming, consider replacing the entire for loop with something like:

const faqC = faqdata.filter(fd => fd[0].includes("Command:")).map(fd => fd[1]);
Sign up to request clarification or add additional context in comments.

Comments

0

The variable j is being used as the index for faqC and causing your issue. If j is 3 but faqC currently contains only one item, index 2 of faqC will be left empty. To solve this issue use faqC.push(faqdata[j][1) so faqC is populated consecutively.

Comments

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.