0.1.2 • Published 6 years ago

group-mysql-value v0.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

用于将mysql联表查询的同一key,不同value合并为value数组形式

// mysqld 查询出的数据
[
  {id: 1, title: 'title1', age: 12},
  {id: 1, title: 'title2', age: 15},
  {id: 2, title: 'title3', age: 10},
  {id: 2, title: 'title4', age: 18},
  {id: 2, title: 'title5', age: 20},
]

通过id将数据合并,并把title和age组合成数组

const groupMysqlValue = require('group-mysql-value')

const value = groupMysqlValue(sourceData, 'id', ['title', 'age'])

console.log(value)
// 输出结果
[ 
  { 
    id: 1, 
    title: [ 'title1', 'title2' ], 
    age: [ 12, 15 ] 
  },
  { 
    id: 2,
    title: [ 'title3', 'title4', 'title5' ],
    age: [ 10, 18, 20 ] 
  }
]