我是Node \ JS的新手,并且坚持如何在创建新集合的集合中合并匹配元素和迭代不匹配元素。到目前为止,我已经使用lodash将下面粘贴的数据分组(“分组输入”),但是无法弄清楚如何获得所需的输出。
我愿意使用lodash \ underscore \ javascript或与Node兼容的任何其他库。
感谢您的帮助和建议。
Grouped Input:
[ { mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM10',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0900' },
{ mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' },
{ mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM11',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '0930' },
{ mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation: 'ROOM12',
mod_AppointmentDate: '10/26/18',
mod_AppointmentTime: '1015' }
]
Desired Output:
[ { mod_AccountNumber: '0001',
mod_Id: '123456',
mod_LastName: 'SMITH',
mod_FirstName: 'NANCY',
mod_AppointmentLocation_1: 'ROOM10',
mod_AppointmentTime_1:'0900',
mod_AppointmentLocation_2: 'ROOM11',
mod_AppointmentTime_2:'0930' },
{ mod_AccountNumber: '0001',
mod_Id: '654321',
mod_LastName: 'JONES',
mod_FirstName: 'NATASHA',
mod_AppointmentLocation_1: 'ROOM11',
mod_AppointmentTime_1:'0930',
mod_AppointmentLocation_2: 'ROOM12',
mod_AppointmentTime_2:'1015' }
]
答案 0 :(得分:1)
const {groupBy, values, merge} = require('lodash');
// input...
const output = values(
groupBy(input, 'id')
).map(chunks => merge(...chunks));
答案 1 :(得分:0)
总体上更好的方法是将这些日期/时间/位置转换为数组。但是鉴于当前的问题,可以这样做:
const data = [{ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' }, { mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' }, { mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' }, { mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' } ]
const result = _(data)
.groupBy('mod_Id')
.values()
.map(x => _.values(_.transform(x, (r,c,i) => {
r[c.mod_Id] = _.extend(r[c.mod_Id], {
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation${i+1}`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate${i+1}`]: c.mod_AppointmentDate,
[`mod_AppointmentTime${i+1}`]: c.mod_AppointmentTime,
})}, {})))
.flatten()
.value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
想法是首先将mod_Id
分组,然后将values
map
穿过它们,然后transform
使其成为一个对象。由于您已恢复为对象形式,因此可以得到values
,最后得到flatten
数组。
更简单的方法是将 ES6 与以下内容一起使用:
const data = [{ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' }, { mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' }, { mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' }, { mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' } ]
const result = Object.values(data.reduce((r, c, i) => {
let _x = r[c.mod_Id] ? r[c.mod_Id]._x : 0
_x++
r[c.mod_Id] = Object.assign(r[c.mod_Id] || {}, {
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
[`mod_AppointmentLocation${_x}`]: c.mod_AppointmentLocation,
[`mod_AppointmentDate${_x}`]: c.mod_AppointmentDate,
[`mod_AppointmentTime${_x}`]: c.mod_AppointmentTime,
_x
})
return r
}, {})).map(({_x, ...rest}) => rest)
console.log(result)
唯一需要注意的是,这种方法并不纯正,因为它通过使用_x
属性对对象进行更改来跟踪当前商品编号,该属性最后通过map
剥离了。 >
如果您想考虑采用数组思想,那么事情会变得简单得多:
const data = [{ mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM10', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0900' }, { mod_AccountNumber: '0001', mod_Id: '123456', mod_LastName: 'SMITH', mod_FirstName: 'NANCY', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' }, { mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM11', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '0930' }, { mod_AccountNumber: '0001', mod_Id: '654321', mod_LastName: 'JONES', mod_FirstName: 'NATASHA', mod_AppointmentLocation: 'ROOM12', mod_AppointmentDate: '10/26/18', mod_AppointmentTime: '1015' } ]
const result = _.chain(data)
.reduce((r,c,i) => {
r[c.mod_Id] = r[c.mod_Id] ? r[c.mod_Id] : {
mod_Id: c.mod_Id,
mod_AccountNumber: c.mod_AccountNumber,
mod_LastName: c.mod_LastName,
mod_FirstName: c.mod_FirstName,
schedule: []
}
r[c.mod_Id].schedule.push({
mod_AppointmentLocation: c.mod_AppointmentLocation,
mod_AppointmentDate: c.mod_AppointmentDate,
mod_AppointmentTime: c.mod_AppointmentTime
})
return r
}, {})
.values()
.value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>