-2
我做了以下內容:找到一個數組中與第一串未定義
<p>1983 (on television), 1984 (in theatres)</p>
的我跑
var d;
var dateText = $("p").text().split(/\s+/g);
for(var i = 0; i < dateText.length; i++) {
d += dateText[i] + ' ';
}
var words = d.replace("–", " ").replace("-", " ").replace(",", " ").replace("/", " ").split(' ');
words = $.grep(words, function(n, i){
return (n !== "" && n != null);
});
var array = words;
var newArray = array.filter(function(v){return v!==''});
console.log(newArray);
["undefined1983", "(on", "television)", "1984", "(in", "theatres)"]
我不應該undefined1983
但1983
另外,如果我這樣做:
var d;
var spacetime = [];
spacetime.push({
Title : [],
Space : [],
Time : {
days : [],
months : [],
years : [],
suffixes : []
},
articleIdPush : [],
originalFullDate : [],
curArtLangPrefix : ["en"],
SingleTranslatedArticle : [],
});
var dateText = $("p").text().split(/\s+/g);
for(var i = 0; i < dateText.length; i++) {
d += dateText[i] + ' ';
}
var words = d.replace("–", " ").replace("-", " ").replace(",", " ").replace("/", " ").split(' ');
words = $.grep(words, function(n, i){
return (n !== "" && n != null);
});
var array = words;
var newArray = array.filter(function(v){return v!==''});
for (const word of newArray) {
if (months.has(word)) {
spacetime[counter].Time.months.push(word);
} else if (+word < 32) {
spacetime[counter].Time.days.push(+word);
} else if (+word < 2200) {
spacetime[counter].Time.years.push(+word);
} else if (/\w+/.test(word)) {
spacetime[counter].Time.suffixes.push(word);
}
}
console.log(spacetime);
我得到
(index):90 Uncaught ReferenceError: months is not defined
我應該有它正確的對象定義的數組。
您期待的輸出是什麼? – abagshaw
我不明白是什麼問題?你想擺脫'undefined'嗎?你想知道它爲什麼在那裏? –
@ N.Ivanov對不起剛剛更新,因爲我包括第二個問題,但情況是類似的 –