我有很多对象,例如
[ { fName: 'Sohail',
lName: 'Afzal',
dob: 1995-07-08T19:00:00.000Z,
gender: 'Male',
phone: '(612)545-8924',
phone1: '(123)456-7890',
payorName: 'hashir',
payorId: 12,
affiliationDate: 2018-10-09T19:00:00.000Z },
{ fName: 'Joann',
mName: 'Denise',
lName: 'Gasner',
dob: 1964-10-13T19:00:00.000Z,
gender: 'Male',
phone: '(612)990-8450',
phone1: '(612)545-8924',
payorName: 'Nouman Latif',
payorId: 15,
affiliationDate: 2018-10-10T19:00:00.000Z } ]
从对象中获取一些数据并存储在数组中,然后再次将数据推入对象中。我需要将最后三个元素分组为单个数组名称payInfo
输出想要
[ { fName: 'Sohail',
lName: 'Afzal',
dob: 1995-07-08T19:00:00.000Z,
gender: 'Male',
phone: '(612)545-8924',
phone1: '(123)456-7890',
payInfo:[{payorName: 'hashir',
payorId: 12,
affiliationDate: 2018-10-09T19:00:00.000Z}]
},
{ fName: 'Joann',
mName: 'Denise',
lName: 'Gasner',
dob: 1964-10-13T19:00:00.000Z,
gender: 'Male',
phone: '(612)990-8450',
phone1: '(612)545-8924',
payInfo:[{payorName: 'Nouman Latif',
payorId: 15,
affiliationDate: 2018-10-10T19:00:00.000Z}]
} ]
需要用lodash解决这个问题。
答案 0 :(得分:3)
您可以执行以下操作:
const data = [{ fName: 'Sohail', lName: 'Afzal', dob: '1995-07-08T19:00:00.000Z', gender: 'Male', phone: '(612)545-8924', phone1: '(123)456-7890', payorName: 'hashir', payorId: 15, affiliationDate: '2018-10-09T19:00:00.000Z' }, { fName: 'Joann', mName: 'Denise', lName: 'Gasner', dob: '1964-10-13T19:00:00.000Z', gender: 'Male', phone: '(612)990-8450', phone1: '(612)545-8924', payorName: 'Nouman Latif', payorId: 15, affiliationDate: '2018-10-10T19:00:00.000Z' } ]
const result = _.map(data, x =>
_.omit(_.extend(x, { payInfo: [{
payorName: x.payorName,
payorId: x.payorId,
affiliationDate: x.affiliationDate
}]
}), ['payorName', 'payorId', 'affiliationDate']))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
使用_.extend
装饰对象,并使用_.omit
删除旧字段。
通过 ES6 ,您可以使用Array.map
和destructuring:
const data = [{ fName: 'Sohail', lName: 'Afzal', dob: '1995-07-08T19:00:00.000Z', gender: 'Male', phone: '(612)545-8924', phone1: '(123)456-7890', payorName: 'hashir', payorId: 15, affiliationDate: '2018-10-09T19:00:00.000Z' }, { fName: 'Joann', mName: 'Denise', lName: 'Gasner', dob: '1964-10-13T19:00:00.000Z', gender: 'Male', phone: '(612)990-8450', phone1: '(612)545-8924', payorName: 'Nouman Latif', payorId: 15, affiliationDate: '2018-10-10T19:00:00.000Z' } ]
const result = data.map(({payorName, payorId, affiliationDate, ...x}) => {
x.payInfo = [{ payorName, payorId, affiliationDate }]
return x
})
console.log(result)
或更简单的通过Object.assign
:
const data = [{ fName: 'Sohail', lName: 'Afzal', dob: '1995-07-08T19:00:00.000Z', gender: 'Male', phone: '(612)545-8924', phone1: '(123)456-7890', payorName: 'hashir', payorId: 15, affiliationDate: '2018-10-09T19:00:00.000Z' }, { fName: 'Joann', mName: 'Denise', lName: 'Gasner', dob: '1964-10-13T19:00:00.000Z', gender: 'Male', phone: '(612)990-8450', phone1: '(612)545-8924', payorName: 'Nouman Latif', payorId: 15, affiliationDate: '2018-10-10T19:00:00.000Z' } ]
const result = data.map(({payorName, payorId, affiliationDate, ...x}) =>
Object.assign(x, {payInfo: [{ payorName, payorId, affiliationDate }]}))
console.log(result)