1.0.3 • Published 4 years ago

m-datas v1.0.3

Weekly downloads
101
License
MIT
Repository
-
Last release
4 years ago

Installation and import

  • Using npm

    npm install m-datas
  • Using yarn

    yarn add m-datas
  • ES6 import mode

    import Data from 'm-datas'

Use examples

  • Object array to object

    const sourceModel = [
      { id: 1, name: '巨量' },
      { id: 2, name: '微信' },
      { id: 3, name: '广点通' }
    ]
    const resultModel = Data.listToMapByValue(sourceModel, 'id')

    Output results

    {
      1: { id: 1, name: '巨量' },
      2: { id: 2, name: '微信' },
      3: { id: 3, name: '广点通' }
    }
  • Merge multidimensional object arrays and remove duplicate

    const sourceModel = [
      { year: "2020", month: 1, startDate: "2020-01-01", endDate: "2020-01-31" },
      { year: "2020", month: 2, startDate: "2020-02-01", endDate: "2020-02-29" },
      { year: "2021", month: 1, startDate: "2021-01-01", endDate: "2021-01-31" },
      { year: "2021", month: 2, startDate: "2021-02-01", endDate: "2021-02-28" }
    ]
    const resultModel = Data.unique(sourceModel, 'year')

    Output results

    [
      {
        year: "2020",
        options: [
          { year: "2020", month: 1, startDate: "2020-01-01", endDate: "2020-01-31" },
          { year: "2020", month: 2, startDate: "2020-02-01", endDate: "2020-02-29" }
        ]
      },
      {
        year: "2021",
        options: [
          { year: "2021", month: 1, startDate: "2021-01-01", endDate: "2021-01-31" },
          { year: "2021", month: 2, startDate: "2021-02-01", endDate: "2021-02-28" }
        ]
      }
    ]
  • List structure to tree structure

    const sourceModel = [
      { id: 1, name: 'M1部门' },
      { id: 11, pid: 1, name: '张三' },
      { id: 12, pid: 1, name: '李四' },
      { id: 13, pid: 1, name: '王五' },
      { id: 2, name: 'M2部门' },
      { id: 21, pid: 2, name: '赵六' },
      { id: 22, pid: 2, name: '周七' },
      { id: 23, pid: 2, name: '吴八' }
    ]
    const resultModel = Data.listToTree(sourceModel, 'id', 'pid')

    Output results

    [
      {
        id: 1,
        name: 'M1部门',
        children: [
          { id: 11, pid: 1, name: '张三' },
          { id: 12, pid: 1, name: '李四' },
          { id: 13, pid: 1, name: '王五' }
        ]
      },
      {
        id: 2,
        name: 'M2部门',
        children: [
          { id: 21, pid: 2, name: '赵六' },
          { id: 22, pid: 2, name: '周七' },
          { id: 23, pid: 2, name: '吴八' }
        ]
      }
    ]

使用示例

  • 对象数组转为对象,如:{ 数组中各个对象的某个字段值: 对象本身 }

    const sourceModel = [
      { id: 1, name: '巨量' },
      { id: 2, name: '微信' },
      { id: 3, name: '广点通' }
    ]
    const resultModel = Data.listToMapByValue(sourceModel, 'id')

    输出结果

    {
      1: { id: 1, name: '巨量' },
      2: { id: 2, name: '微信' },
      3: { id: 3, name: '广点通' }
    }
  • 合并多维对象数组并去重

    const sourceModel = [
      { year: "2020", month: 1, startDate: "2020-01-01", endDate: "2020-01-31" },
      { year: "2020", month: 2, startDate: "2020-02-01", endDate: "2020-02-29" },
      { year: "2021", month: 1, startDate: "2021-01-01", endDate: "2021-01-31" },
      { year: "2021", month: 2, startDate: "2021-02-01", endDate: "2021-02-28" }
    ]
    const resultModel = Data.unique(sourceModel, 'year')

    输出结果

    [
      {
        year: "2020",
        options: [
          { year: "2020", month: 1, startDate: "2020-01-01", endDate: "2020-01-31" },
          { year: "2020", month: 2, startDate: "2020-02-01", endDate: "2020-02-29" }
        ]
      },
      {
        year: "2021",
        options: [
          { year: "2021", month: 1, startDate: "2021-01-01", endDate: "2021-01-31" },
          { year: "2021", month: 2, startDate: "2021-02-01", endDate: "2021-02-28" }
        ]
      }
    ]
  • 列表结构转为树结构

    const sourceModel = [
      { id: 1, name: 'M1部门' },
      { id: 11, pid: 1, name: '张三' },
      { id: 12, pid: 1, name: '李四' },
      { id: 13, pid: 1, name: '王五' },
      { id: 2, name: 'M2部门' },
      { id: 21, pid: 2, name: '赵六' },
      { id: 22, pid: 2, name: '周七' },
      { id: 23, pid: 2, name: '吴八' }
    ]
    const resultModel = Data.listToTree(sourceModel, 'id', 'pid')

    输出结果

    [
      {
        id: 1,
        name: 'M1部门',
        children: [
          { id: 11, pid: 1, name: '张三' },
          { id: 12, pid: 1, name: '李四' },
          { id: 13, pid: 1, name: '王五' }
        ]
      },
      {
        id: 2,
        name: 'M2部门',
        children: [
          { id: 21, pid: 2, name: '赵六' },
          { id: 22, pid: 2, name: '周七' },
          { id: 23, pid: 2, name: '吴八' }
        ]
      }
    ]
1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago