1.0.4 • Published 1 year ago

ljt-tools v1.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

安装

npm install ljt-tools

导入

const tools = require('ljt-tools')

数组去重

// 方式一
const arr = [1,1,2,2,3,4];
console.log(tools.removeArrRepeat(arr)) // [1,2,3]

// 方式二
const arr1 = [
  {name: '张三', id: 1},
  {name: '张三', id: 1},
  {name: '李四', id: 2},
  {name: '李四', id: 2}
];
// 根据相同的 id 去重
console.log(tools.removeArrRepeat(arr1, 'id')); // [{name: '张三', id: 1},{name: '李四', id: 2}]

数组排序

const arr = [2, 3, 1, 7, 4, 8, 5]
console.log(tools.arrSort(arr, 'up'));

const arr1 = [
  { name: '张三', age: '10' },
  { name: '李四', age: '6' },
  { name: '王五', age: '20' },
  { name: '马六', age: '3' },
]
console.log(tools.arrSort(arr1, 'up', 'age'));

对象深拷贝

const obj = {
  name: 'zs',
  age: 18,
  job: {
    work: 'driver',
    hobby: ['study', 'movie']
  },
  say () {
    console.log('hello');
  }
}
const obj1 = deepClone(obj);
obj1.name = 'ls';
console.log(obj);
// {
//   name: 'zs',
//   age: 18,
//   job: { work: 'driver', hobby: [ 'study', 'movie' ] },
//   say: [Function: say]
// }
console.log(obj1);
// {
//   name: 'ls',
//   age: 18,
//   job: { work: 'driver', hobby: [ 'study', 'movie' ] },
//   say: [Function: say]
// }

递归寻找子类的父类

// dic 数据中,当没有下级时,默认 children: []
const dic = [
  {
    label: '一级',
    value: 1,
    id: '1',
    parentId: '',
    children: [
      {
        label: '一级/一',
        value: 1,
        id: '11',
        parentId: '1',
        children: []
      },
      {
        label: '一级/二',
        value: 2,
        id: '22',
        parentId: '1',
        children: []
      }
    ]
  },
  {
    label: '二级',
    value: 2,
    id: '2',
    parentId: '',
    children: [
      {
        label: '二级/一',
        value: 1,
        id: '33',
        parentId: '2',
        children: []
      },
      {
        label: '二级/二',
        value: 2,
        id: '44',
        parentId: '2',
        children: []
      }
    ]
  }
]
console.log(tools.findParent(dic, '44'));
// {
//   label: '二级',
//   value: 2,     
//   id: '2',      
//   parentId: '', 
//   children: [
//     { label: '二级/一', value: 1, id: '33', parentId: '2', children: [] },
//     { label: '二级/二', value: 2, id: '44', parentId: '2', children: [] }
//   ]
// }

递归找出树结构中每层所能匹配到value的label组成的字符串

/**
 * 递归找出树结构中每层所能匹配到value的label组成的字符串
 * @param {Tree} node 树结构数据
 * @param {Array} value 树结构每层的 value ['11', '22', '33']
 * @param {Array} names 空数组,用于接收
 * @param {Number} index 
 * @param {Object} option 配置项,可配置,默认 {label: 'label', value: 'value', children: 'children', separate: ','}, 与自己树结构中字段对应就行,separate 规定什么字符 隔开
 * @returns 返回一个由树结构每层所匹配的 label 组成的字符串
 */

let dic = [
  {
    id: '0', label1: '一级', value: '0', children: [
      { id: '01', label1: '一级/一', value: '01' },
      { id: '02', label1: '一级/二', value: '02' },
      { id: '03', label1: '一级/三', value: '03' }
    ]
  },
  {
    id: '1', label1: '二级', value: '1', children: [
      { id: '11', label1: '二级/一', value: '11' },
      { id: '12', label1: '二级/二', value: '12' },
      {
        id: '13', label1: '二级/三', value: '13', children: [
          { id: '131', label1: '二级/三/一', value: '131' }
      ] }
    ]
  }, {
    id: '2', label1: '三级', value: '2', children: [
      { id: '21', label1: '三级/一', value: '21' },
      { id: '22', label1: '三级/二', value: '22' },
      { id: '23', label1: '三级/三', value: '23' }
    ]
  }
]

console.log(findNames(dic, ['1', '13', '131'], [], 0, { separate: '|', label: 'label1' }));
// 二级|二级/三|二级/三/一

防抖

// vue
fn: debounce(function () {
  console.log(1);
}, 500)

节流

fn: throttle(function () {
  console.log(1);
}, 500)

开源协议

ISC

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago