我已經看了一些已經存在的堆棧溢出問題的例子,但我不斷收到「未定義」,似乎是循環訪問數組中的每一個字符。循環通過JS對象數組的特定屬性
我不是太清楚如何阻止這種,所以我可以有它只是打印出帶有「名稱」屬性
CSV示例一切:
[{"Extension":"071","Company Name":"test1","Name":"Iron"},{"Extension":"072","Company Name":"test2","Name":"Blue"},{"Extension":"073","Company Name":"test3","Name":"Bloggs"},{"Extension":"074","Company Name":"test4","Name":"Best"},{"Extension":"075","Company Name":"test5","Name":"Green"}]
客戶端代碼:
$("#CSV-Upload").click(function() {
$("input[type=file]").parse({
config: {
header: true,
skipEmptyLines: true,
complete: function(results, file) {
console.log("This file done:", file, results);
var string = JSON.stringify(results['data']);
result.push(string);
console.log("CSV Array: " + string);
socket.emit('CSVSQL', string);
}
},
complete: function() {
console.log("All files done!");
}
});
$("#csv-file").val('');
});
服務器端代碼:
function CSVSQL(csvdata) {
if (csvdata.length > 0) {
console.log('Current CSV data Information: \n');
console.log(csvdata);
for (var j = 0; j < csvdata.length; j++){
console.log(csvdata[j].Name);
}
} else {
console.log('No data in that CSV file :-(\n');
};
}
io.sockets.on('connection', function (socket) {
socket.on('CSVSQL', function (csvdata) {
CSVSQL(csvdata);
});
});
任何人都可以解釋我哪裏出了問題,並提供一些建議?
當你'的console.log(csvdata);',你會得到什麼輸出? –
@PrashantAgrawal當我這樣做時,它打印出整個數組幾百次。 – Studento919