将对象推送到数组使其未定义——作用域问题?
Pushing an object to an array makes it undefined--Scoping issue?
我正在编写一个网络抓取工具,它使用正则表达式提取段落中的信息并将其存储在一个对象中。然后我将对象添加到数组中。这是我的完整代码:
function scrapeCourseData(htmlString) {
// scrapes a specific department's course list
var tempArr = [];
console.log(tempArr); // outputs '[]'
$ = cheerio.load(htmlString);
// #coursestextcontainer contains the actual information for every single course listed in a department
$('#coursestextcontainer').find('.courseblock').each(function(i, elem) {
// finds all divs of type courseblock, iterates though each of them,
// extracting course information from children.
console.log('courseblock ' + (i + 1));
var courseText = $('strong', '.courseblocktitle', elem).text(); // Gets the text that will be parsed
var regex = /([A-Z]{4}\s[A-Z]{1,2}\d{4})\s(.*?)(?:\.*)(\d{1,2}(?:\.?|-?)\d{0,2}\spoints?)/g;
var regexGroups = Object.freeze({
NUMBER: 1,
NAME: 2,
CREDITS: 3
});
var match, course;
while ((match = regex.exec(courseText)) !== null) { // when regex.exec returns null, no more matches, and loop stops.
course = {
number: match[regexGroups.NUMBER],
name: match[regexGroups.NAME],
credits: match[regexGroups.CREDITS]
};
tempArr.push(course); // doesn't work-- result is array full of 'null'
console.log(course); // but this outputs as a valid object, e.g. { number: 'AFAS W3030'... }
}
});
console.log("Complete tempArr: " + tempArr); // outputs [object Object],[object Object],[object Object], etc.
for (var j of tempArr) {
dataJSONObject.push(tempArr[j]);
console.log('\ntempArray at ' + j + ': ' + tempArr[j]); // outputs [object Object]: undefined
}
console.log('\n');
}
当我第一次将tempArr
定义为[]
并将其输出到控制台时,我得到了预期的结果[]
。
我从正则表达式匹配中形成的对象在运行时也如预期的那样有效。
然而,当我尝试将这些对象推送到 tempArr
,然后打印 tempArr
,它输出为 undefined
。
我一直在研究其他 Whosebug 问题,我很确定我的问题是当我推送到 tempArr
时,我这样做超出了它的范围。我试过在我声明 tempArr
的地方四处移动(例如,通过将它放在它的函数之外以使其成为全局),但我在推送后仍然得到 undefined
。我错过了什么?
你推入数组的对象在那里,你只是错误地从数组中读出。 for...of
循环不会将索引值放入您提供的变量中,它会将 value 放入。这就解释了为什么 tempArr[j]
是 undefined
.
将您的 for...of
循环更改为:
for (var j of tempArr) {
dataJSONObject.push(j);
console.log('\ntempArray: ' + j);
}
此外,另一种将一个数组的所有元素放入另一个数组的方法是使用 spread syntax:
dataJSONObject.push(...tempArr);
我正在编写一个网络抓取工具,它使用正则表达式提取段落中的信息并将其存储在一个对象中。然后我将对象添加到数组中。这是我的完整代码:
function scrapeCourseData(htmlString) {
// scrapes a specific department's course list
var tempArr = [];
console.log(tempArr); // outputs '[]'
$ = cheerio.load(htmlString);
// #coursestextcontainer contains the actual information for every single course listed in a department
$('#coursestextcontainer').find('.courseblock').each(function(i, elem) {
// finds all divs of type courseblock, iterates though each of them,
// extracting course information from children.
console.log('courseblock ' + (i + 1));
var courseText = $('strong', '.courseblocktitle', elem).text(); // Gets the text that will be parsed
var regex = /([A-Z]{4}\s[A-Z]{1,2}\d{4})\s(.*?)(?:\.*)(\d{1,2}(?:\.?|-?)\d{0,2}\spoints?)/g;
var regexGroups = Object.freeze({
NUMBER: 1,
NAME: 2,
CREDITS: 3
});
var match, course;
while ((match = regex.exec(courseText)) !== null) { // when regex.exec returns null, no more matches, and loop stops.
course = {
number: match[regexGroups.NUMBER],
name: match[regexGroups.NAME],
credits: match[regexGroups.CREDITS]
};
tempArr.push(course); // doesn't work-- result is array full of 'null'
console.log(course); // but this outputs as a valid object, e.g. { number: 'AFAS W3030'... }
}
});
console.log("Complete tempArr: " + tempArr); // outputs [object Object],[object Object],[object Object], etc.
for (var j of tempArr) {
dataJSONObject.push(tempArr[j]);
console.log('\ntempArray at ' + j + ': ' + tempArr[j]); // outputs [object Object]: undefined
}
console.log('\n');
}
当我第一次将tempArr
定义为[]
并将其输出到控制台时,我得到了预期的结果[]
。
我从正则表达式匹配中形成的对象在运行时也如预期的那样有效。
然而,当我尝试将这些对象推送到 tempArr
,然后打印 tempArr
,它输出为 undefined
。
我一直在研究其他 Whosebug 问题,我很确定我的问题是当我推送到 tempArr
时,我这样做超出了它的范围。我试过在我声明 tempArr
的地方四处移动(例如,通过将它放在它的函数之外以使其成为全局),但我在推送后仍然得到 undefined
。我错过了什么?
你推入数组的对象在那里,你只是错误地从数组中读出。 for...of
循环不会将索引值放入您提供的变量中,它会将 value 放入。这就解释了为什么 tempArr[j]
是 undefined
.
将您的 for...of
循环更改为:
for (var j of tempArr) {
dataJSONObject.push(j);
console.log('\ntempArray: ' + j);
}
此外,另一种将一个数组的所有元素放入另一个数组的方法是使用 spread syntax:
dataJSONObject.push(...tempArr);