/**
* 安装node-xlsx插件
*/
var path = require('path')
var fs = require('fs')
var xlsx = require('node-xlsx')
//去重算法
Array.prototype.unique = function() {
this.sort(); //先排序
var res = [];
let obj = {};
res = this.reduce((prev, cur) => {
console.log(cur, 'cur')
obj[cur] ? '' : obj[cur] = true && prev.push(cur);
return prev;
}, []) //设置cur默认类型为数组,并且初始值为空的数组
console.log(res, '去重了')
return res;
}
//取得xlsx
var obj = xlsx.parse(path.resolve(`./111.xls`))
var newArray = obj[0].data;
//读取第一列
//obj[0].data:指第一个sheet的表格数据
//data内部的数据结构为:
//[[ 'field1','field2','field13' ],[ 'field1','field2','field13' ]]
// for (var data of obj[0].data) {
// console.log(data)
// newArray.push(data)
// }
var openIds = newArray.unique();
writeExcel('去重后的数据', openIds)
function writeExcel(name, data) {
var buffer = xlsx.build([{
name: 'sheet1',
data: data
}]);
fs.writeFileSync(path.join(__dirname, name + '.xlsx'), buffer, {
'flag': 'w'
});
console.log('导出成功')
}
nodejs 去重
最新推荐文章于 2023-03-01 07:00:00 发布