简体   繁体   中英

Javascript array fills with empty items

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...

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]);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM