| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- const tools = {
- isNotEmpty: (val) => {
- return typeof val !== 'undefined' && val !== null ? true : false
- },
- isEmpty: (val) => {
- return val === null && typeof val === 'undefined' ? true : false
- },
- isBlack: (val) => {
- let regular = new RegExp('^[ ]+$')
- return (
- val == null || typeof val == 'undefined' || val == '' || regular.test(val)
- )
- },
- data2Tree: (list, topValue, pName, name) => {
- if (tools.isEmpty(list) || list.length === 0) {
- return list
- }
- if (tools.isBlack(topValue)) {
- topValue = '0'
- }
- if (tools.isBlack(pName)) {
- pName = 'pid'
- }
- if (tools.isBlack(name)) {
- name = 'id'
- }
- return tools.createTree(list, topValue, pName, name)
- },
- createTree: (list, pid, pName, name) => {
- let tree = []
- for (let num = 0; num < list.length; num++) {
- let data = list[num]
- if (data[pName] === pid) {
- data['children'] = tools.createTree(list, data[name], pName, name)
- tree.push(data)
- }
- }
- return tree
- },
- /**
- * map 排序对 map 的 key 值进行排序
- * @param obj
- */
- sortMap: (obj) => {
- // 1.根据数组中的对象,得到排序后的 key,return key2-key1 表示降序
- let newkey = Object.keys(obj).sort((key1, key2) => {
- return obj[key2] - obj[key1]
- })
- // 2.用排序后的key构建新的对象数组
- let newObj = {}
- // 创建一个新的对象,用于存放排好序的键值对
- for (var i = 0; i < newkey.length; i++) {
- // 遍历 newkey 数组, 向新创建的对象中按照排好的顺序依次增加键值对
- newObj[newkey[i]] = obj[newkey[i]]
- }
- return newObj //返回排好序的新对象
- },
- findKey: (data, value) => {
- let compare = (a, b) => a === b
- return Object.keys(data).find((k) => compare(data[k], value))
- },
- /** 数组拷贝 */
- newObjectArrayWithDeepCopy: (items) => {
- var newObjectArray = new Array()
- for (var itemIndex = 0; itemIndex < items.length; itemIndex++) {
- newObjectArray[itemIndex] = deepCopy(items[itemIndex])
- }
- return newObjectArray
- },
- /** 对象拷贝 */
- newObjectWithDeepCopy: (object) => {
- let newObject = {}
- newObject = JSON.parse(JSON.stringify(object))
- // for (attribute in object) {
- // if (typeof object.attribute === 'object' && object.attribute != null) {
- // newObject.attribute = deepCopy(object.attribute)
- // } else {
- // newObject.attribute = object.attribute
- // }
- // }
- return newObject
- },
- }
- export default tools
|