运营人员通常会用Excel表格统计数据,于是我们要开发一个功能就是自动 导入Excel数据。目前通过nodejs的node-xlsx模块可以读取Excel数据。以下是 通过该模块读取表格部分数据的返回: ar data=[ name:'sheet1',表 data:[ 标题 r'id'. 'name', 'phone', 'pay', 'email', 'address'], '北京海淀'], [`12° xiaoming "123456' '23000', '123@123.com '浙江杭州"], 77 "xiaohua', '345612', "13000', "223@223.com', "湖北武汉'], [516' 'xiaoli', '123756', '26700', '233@233.com', ["182" 'xiaoxiang" '873456', '30600','113@113. com', '江西南昌'] ['335', "xiaoqiang','125556', '72000','133@133.com', '广东深圳'] name: 'sheet2', data: [] 现在要对表一中的数据生成JSON 对象用于数据导入,形如: "id":"12", "name": "xiaoming", "phone":"123456", "pay": "23000", "email":"123@123.com" "address":"北京海淀" }, 请完成数据处理。
时间: 2023-12-30 17:03:25 浏览: 117
好的,根据您给出的数据,我们可以进行如下处理,生成JSON对象:
```javascript
let data = [
{
name: 'sheet1',
data: [
['id', 'name', 'phone', 'pay', 'email', 'address'],
['12', 'xiaoming', '123456', '23000', '123@123.com', '浙江杭州'],
['77', 'xiaohua', '345612', '13000', '223@223.com', '湖北武汉'],
['516', 'xiaoli', '123756', '26700', '233@233.com', '江西南昌'],
['182', 'xiaoxiang', '873456', '30600', '113@113.com', '广东深圳'],
['335', 'xiaoqiang', '125556', '72000', '133@133.com', '北京海淀']
]
}
];
let result = [];
for (let i = 1; i < data[0].data.length; i++) {
let obj = {};
for (let j = 0; j < data[0].data[i].length; j++) {
obj[data[0].data[0][j]] = data[0].data[i][j];
}
result.push(obj);
}
console.log(result);
```
运行结果:
```javascript
[
{
id: '12',
name: 'xiaoming',
phone: '123456',
pay: '23000',
email: '123@123.com',
address: '浙江杭州'
},
{
id: '77',
name: 'xiaohua',
phone: '345612',
pay: '13000',
email: '223@223.com',
address: '湖北武汉'
},
{
id: '516',
name: 'xiaoli',
phone: '123756',
pay: '26700',
email: '233@233.com',
address: '江西南昌'
},
{
id: '182',
name: 'xiaoxiang',
phone: '873456',
pay: '30600',
email: '113@113.com',
address: '广东深圳'
},
{
id: '335',
name: 'xiaoqiang',
phone: '125556',
pay: '72000',
email: '133@133.com',
address: '北京海淀'
}
]
```
这样,我们就成功将表一中的数据生成了JSON对象。
阅读全文