Javascript 访问JSON数组数据
因此,我试图将数据从excel文件获取到一个表中,我可以将数据从excel获取到JSON格式,但现在我尝试使用这些数据进行输出 我使用此函数将excel转换为JSON格式:Javascript 访问JSON数组数据,javascript,arrays,json,excel,sorting,Javascript,Arrays,Json,Excel,Sorting,因此,我试图将数据从excel文件获取到一个表中,我可以将数据从excel获取到JSON格式,但现在我尝试使用这些数据进行输出 我使用此函数将excel转换为JSON格式: var ExcelToJSON = function() { this.parseExcel = function(file) { var reader = new FileReader(); reader.onload = function(e) {
var ExcelToJSON = function() {
this.parseExcel = function(file) {
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
var workbook = XLSX.read(data, {
type: 'binary'
});
workbook.SheetNames.forEach(function(sheetName) {
// Here is your object
var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
var json_object = JSON.stringify(XL_row_object);
console.log(JSON.parse(json_object));
jQuery( '#xlx_json' ).val( json_object );
})
};
reader.onerror = function(ex) {
console.log(ex);
};
reader.readAsBinaryString(file);
};
};
它将输出如下内容:
const data =
[ { Rep: 'aa'
, Type: 'SO'
, Number: '11'
, Client: 'test'
, Revenue: '4025'
, Equipment: '-2240'
, Share: '0'
, Other: '0'
, Date: '15-5-2020'
, Paidinfulldate: '10-7-2020'
}
, { Rep: 'aa'
, Type: 'SO'
, Number: '111'
, Client: 'test'
, Revenue: '7104'
, Equipment: '-5334.4704'
, Share: '0'
, Other: '0'
, Date: '25-5-2020'
, Paidinfulldate: '16-7-2020'
}
, { Rep: 'aa'
, Type: 'SO'
, Number: '111'
, Client: 'test'
, Revenue: '675'
, Equipment: '-400'
, Share: '0'
, Other: '0'
, Date: '2-6-2020'
, Paidinfulldate: '10-7-2020'
}
, { Rep: 'aa'
, Type: 'SO'
, Number: '111'
, Client: 'test'
, Revenue: '2000'
, Equipment: '-1200'
, Share: '0'
, Other: '0'
, Date: '9-6-2020'
, Paidinfulldate: '10-7-2020'
}
, { Rep: 'aa'
, Type: 'SO'
, Number: '111'
, Client: 'test'
, Revenue: '770'
, Equipment: '-336'
, Share: '0'
, Other: '0'
, Date: '15-6-2020'
, Paidinfulldate: '16-7-2020'
}
, { Rep: 'aa'
, Type: 'SO'
, Number: '111'
, Client: 'test'
, Revenue: '0'
, Equipment: '0'
, Share: '0'
, Other: '15'
, Date: '20-7-2020'
}
]
但在此之后,我想访问这些数据,在其他函数中使用这些数据进行计算,例如收入
我对javascript非常陌生,所以我希望这不是一个愚蠢的问题。一旦你有了数组对象“ExcelToJSON”,你可以通过首先进入数组来访问其中的信息:
ExcelToJSON[0]
这将允许您访问第一个数组中的元素。然后,您可以轻松地使用对象语法
console.log(ExcelToJSON[0].Rep)
上述预期结果:“aa”
下面是使用代码段的示例:
var excelToJSON=[{“代表”:“aa”,“类型”:“SO”,“编号”:“11”,“客户”:“测试”,“收入”:“4025”,“设备”:“-2240”,“共享”:“0”,“其他”:“0”,“日期”:“15-5-2020”,“Paidinfulldate”:“10-7-2020”},{“代表”:“aa”,“类型”:“SO”,“编号”:“111”,“客户”:“测试”,“收入”:“7104”,“设备”:“-5334.4704”,“共享”:“0”,“其他”:“0”,“日期”:“25-5-2020”,“Paidinfulldate”:“16-7-2020”},{“代表”:“aa”,“类型”:“SO”,“编号”:“111”,“客户”:“测试”,“收入”:“675”,“设备”:“-400”,“份额”:“0”,“其他”:“0”,“日期”:“2-6-2020”,“Paidinfulldate”:“10-7-2020”},{“代表”:“aa”,“类型”:“SO”,“编号”:“111”,“客户”:“测试”,“收入”:“2000”,“设备”:“-1200”,“份额”:“0”,“其他”:“0”,“日期”:“9-6-2020”,“Paidinfulldate”:“10-7-2020”{“代表”:“,”类型“:”SO“,”编号“:”111“,”客户“:”测试“,”收入“:”770“,”设备“:”336“,”份额“,”0“,”其他“,”0“,”日期“,”15-6-2020“,”Paidinfulldate“,”16-7-2020“},{”代表“:”aa“,”类型“:”SO“,”编号“:”111“,”客户“:”测试“,”收入“:”0“,”设备“,”0“,”份额“,”0“,”其他“,”15“,”日期“:”20-7-2020”}]
excelToJSON.forEach(thingy=>{console.log(thingy.Revenue)})
var数组=[{“代表”:“aa”,“类型”:“SO”,“编号”:“11”,“客户”:“测试”,“收入”:“4025”,“设备”:“-2240”,“份额”:“0”,“其他”:“0”,“日期”:“15-5-2020”,“Paidinfulldate”:“10-7-2020”},{“代表”:“aa”,“类型”:“SO”,“编号”:“111”,“客户”:“测试”,“收入”:“7104”,“设备”:“-5334.4704”,“份额”:“0”,“其他”:“0”,“日期”:“25-5-2020”,“Paidinfulldate”:“16-7-2020”}{“代表”:“aa”,“类型”:“SO”,“编号”:“111”,“客户”:“测试”,“收入”:“675”,“设备”:“-400”,“份额”:“0”,“其他”:“0”,“日期”:“2-6-2020”,“Paidinfulldate”:“10-7-2020”},{“代表”:“aa”,“类型”:“SO”,“编号”:“111”,“客户”:“测试”,“收入”:“2000”,“设备”:“-1200”,“份额”:“0”,“其他”:“0”,“日期”:“9-6-2020”,“Paidinfulldate”:“10-7-2020”},{“代表”:“aa”,“类型”:“SO”编号“:”111“,”客户“:”测试“,”收入“:”770“,”设备“:”336“,”份额“:”0“,”其他“,”日期“:”15-6-2020“,”Paidinfulldate“:”16-7-2020“},{”代表“:”aa“,”类型“:”SO“,”编号“:”111“,”客户“,”测试“,”收入“:”0“,”设备“:”0“,”份额“,”0“,”其他“:”15“,”日期“:”20-7-2020“}]
对于(var i=0;i
当我尝试实现此功能时,我收到一个错误,表示无法读取未定义的“收入”示例。在我发布此答案后,您的问题已被编辑。您应该使用数据[0]。收入。