3D数组覆盖其他维度的值JS

我似乎无法弄清楚我昨天写的这段代码我做错了什么。 这是我第一次鞭打JavaScript,第一次使用jQuery和Node.js,我会认为这个三维数组应该是原样。 我已经看到混淆提到什么multidimensional array和人们说,JavaScript没有任何,虽然它有arrays的数组。 无论如何,我想我在技术上使用一个数组的数组,并不明白为什么我的外部数组,我认为是一个外部维度的devise,overwrite从两个内部数组的元素到自己的元素。 两个内部数组似乎按照它们应该的方式工作,但是最外部的数组以某种我不了解的方式混合数据。

不一致/问题可以通过滚动这个代码生成的output.json文件来观察,并且看到输出显然不匹配这个网页上的三个表格:

// My server.js file: var express = require('express'); var fs = require('fs'); var request = require('request'); var cheerio = require('cheerio'); var app = express(); // the link below is a tutorial I was loosely following // see http://scotch.io/tutorials/javascript/scraping-the-web-with-node-js app.get('/scrape', function(req, res) { url = 'http://espn.go.com/nba/player/stats/_/id/4145/kareem-abdul-jabbar' request(url, function(error, response, html) { if(!error) { // utilize the Cheerio library on the returned html, yielding jQuery functionality var $ = cheerio.load(html); var numOfRows; var stats = [[[]]]; for(var chart = 0; chart < 3; chart++) { stats.push([[]]); // allocates space for each grid on each chart (each set of rows and columns) $('.tablehead').eq(chart).filter(function(){ var data = $(this); numOfRows = data.children().length - 2; for(var i = 0; i < numOfRows + 1; i++) { stats[chart].push([]); // allocates space for each row in the chart } }) var numOfColumns; $('.stathead').eq(chart).filter(function(){ var data = $(this); stats[chart][0][0] = data.children().first().text(); }) $('.colhead').eq(chart).filter(function(){ // first() specifies to select the first of the three occurances of this class; use eq(param) to find the Nth occurance var data = $(this); numOfColumns = data.children().length; for(var i = 0; i < numOfColumns; i++) { stats[chart][1][i] = data.children().eq(i).text(); } }) var currentRow = 2; for(var oddRow = 0; oddRow < (numOfRows + 1)/2 - 1; oddRow++) { $('.tablehead .oddrow').eq(oddRow).filter(function(){ var data = $(this); for(var c = 0; c < numOfColumns; c++) { stats[chart][currentRow][c] = data.children().eq(c).text(); } currentRow += 2; }) } currentRow = 3; for(var evenRow = 0; evenRow < (numOfRows + 1)/2 - 1; evenRow++){ $('.tablehead .evenrow').eq(evenRow).filter(function(){ var data = $(this); for(var c = 0; c < numOfColumns; c++) { stats[chart][currentRow][c] = data.children().eq(c).text(); } currentRow += 2; }) } currentRow -= 1; // gets the last allocated row index (after "currentRow += 2" has been executed) $('.tablehead .total').eq(chart).filter(function(){ var data = $(this); var LOGOIDX = 1; for(var c = 0; c < numOfColumns - 1; c++) { if(c < LOGOIDX) { stats[chart][currentRow][c] = data.children().eq(c).text(); } if(c == LOGOIDX) { stats[chart][currentRow][c] = "NA"; stats[chart][currentRow][c + 1] = data.children().eq(c).text(); continue; } else { stats[chart][currentRow][c + 1] = data.children().eq(c).text(); } } }) } // end chart loop } // Want to parse my json so that it displays in format: "name: value" rather than just "name" as it is now... fs.writeFile('output.json', JSON.stringify(stats, null, 4), function(err){ console.log('File successfully written! - Check the project directory for the output.json file'); console.log('Number of columns in chart is: ' + numOfColumns); }) // message to browser reminding that there's no UI involved here. res.send('Check the console!') }) }) app.listen('8081') console.log('Magic happens on port 8081'); exports = module.exports = app; 

啊哈! 抓住了我的错误 – 只是一个简单的逻辑错误。 有点尴尬,我没有看到它,但呃,在一天结束的时候做了一些练习和研究(和相当多的分心):

可以看出,除了我在每个图表中search奇数行和偶数行的地方外,我所做的所有searchHTML类的参数都是由一个名为“chart”的variables来参数化的,所以它天真地出现,我的“三维数组[是]从其他维度覆盖值”< – 哈哈。

简而言之,我只需要根据每个图表的条件(一些额外的代码行)创build偏移量,并且需要编辑两行代码以反映新计算的偏移量,如下所示:

$('.tablehead .oddrow').eq(rowOffset + oddRow).filter(function(){

$('.tablehead .evenrow').eq(rowOffset + evenRow).filter(function(){

感谢任何帮助! 我希望这个问题切合实际地使其他人受益:P