UNPKG

tree-structure-merging

Version:

```javascript npm i tree-structure-merging ```

174 lines (152 loc) 4.37 kB
# 1、下载包 ```javascript npm i tree-structure-merging ``` # 2、使用 ```javascript //以对象的形式存放方法,可以结构也可以完全取出 const {treeDate,time,Money} || name? = require("tree-structure-merging"); ``` ## 2-1 合并数组对象相同项 ```javascript //treeDate是用于解决数组对象中存在着相同的键名,需要以这个相同键名为目标合并成一个个数组的形式 //三个参数,data(数据),element(相同的键名),sortElement(可选项,根据哪个键名进行排序,一般用于字母排序) //结构方式 treeDate.SameTreeData(data, element, sortElement); //完全取出方式 name?.treeDate.SameTreeData(data, element, sortElement); //参考数据 let data = [ { type: "Y", data: "为去问问去恶趣味" }, { type: "S", data: "asdadasasa" }, { type: "Y", data: 32423423523 }, { type: "A", data: 213312412 }, { type:"S", data:"zxczxczxczx" } ]; console.log(treeDate.SameTreeData(data, "type", "type")); ``` ## 2-2 时间处理 ```javascript //用法同上 //获取两个日期之间还有几天 //date,otherDate参数分别为两个时间戳 time.diffDays(date, otherDate); //将日期转换成YYYY-MM-DD格式 //date参数传入一个时间戳 time.formatYmd(date); //将数组对象或对象或字符串转换成需要的时间,格式为(年-月-日 时:分:秒); //三个参数 data(数据) str(时间数据) format(需要的日期格式) //注:如果数据是时间戳需要先new Date()转换,也可以用formatYmd方法;如果不是数组或对象而且是Date类型只需要传入两个参数(data,format) //format的格式(yyyy(年)-mm(月)-dd(日) hh(时):ff(分):ss(秒)),分隔符"-"与":"会在传入格式后自动补充 time.dateChange(data, str, format); //参考数据 let data1 = [{ id: 1, time: new Date(), chiidren: [{ id: 1 - 1, time: new Date() }, { id: 1 - 2, time: new Date() }, ] }, { id: 2, time: new Date() }, { id: 3, time: new Date(), chiidren: [{ id: 3 - 1, time: new Date() }, { id: 3 - 2, time: new Date(), chiidren: [{ id: 3 - 2 - 1, time: new Date() }, { id: 3 - 2 - 2, time: new Date() }, ] }, ] }, { id: 4, time: new Date() }, { id: 5, time: new Date() }, ]; let data2 = { id: 1, time: new Date() }; let data3 = new Date(); console.log(time.dateChange(data1, "time", "yyyymmdd")); console.log(time.dateChange(data2, "time", "yyyymmdd hhffss")); console.log(time.dateChange(data3, "yyyymm")); ``` ## 2-3 数字转中文 ```javascript //一个参数 num(传入一个数字) Money.toChineseNum(num); console.log(3992);//三千九百九十二 // 单选计算总价与总数量 //传入四个参数,data(数据),price(指对象中价格的键名),num(指对象中数量的键名),clicked(可选项,指对象中是否选中的键名,如果没有传则直接在每一个对象中添加一个clicked值) Money.totalPrice(data, price, num, clicked); // 全选计算总价与数量 // 同上 Money.selectAll(data, price, num, clicked); //参考数据 let CartList = [{ id: 1, price: 200, num: 2 }, { id: 2, price: 100, num: 1 }, { id: 3, price: 20, num: 1 }, { id: 4, price: 10, num: 10, } ]; ``` # 3、作者为初学者,如果方法写的不好,请见谅! ## 3-1 有需要修正的地方的可以给作者提供,大家互相学习 ## QQ:337477044 ## 邮箱:337477044@qq.com