var data = [
{ 'Promotions 1': 2192, 'dates': '2021-02-04' },
{ 'Promotions 23': 767, 'dates': '2021-02-06' },
{ 'Promotions 12': 2264,'dates': '2021-02-08' },
];
The Issue is Promotions are dynamic key only access dates key-value. I want output like this below. I want that on unique every dates with all promotions Expected Output
var totalUserJoined = [
{
'Promotions 1': 2192,
'Promotions 23': 767,
'Promotions 12': 2264 ,
'dates': '2021-02-04'
},
{
'Promotions 1': 2192,
'Promotions 23': 767,
'Promotions 12': 2264 ,
'dates': '2021-02-06'
},
{
'Promotions 1': 2192,
'Promotions 23': 767,
'Promotions 12': 2264 ,
'dates': '2021-02-08'
}
];
You can use .map()
and destructuring assignment to get all keys other than dates
into their own object. Once you have an array of objects only containing the promotion keys (ie: the none dates
), you can merge them all together using Object.assign()
to form one object. Then you can use .map()
on your original data
array to merge the promotions object with every date:
const data = [ { 'Promotions 1': 2192, 'dates': '2021-02-04' }, { 'Promotions 23': 767, 'dates': '2021-02-06' }, { 'Promotions 12': 2264,'dates': '2021-02-08' }, ]; const promotions = Object.assign(...data.map(({dates, ...rest}) => rest)); const res = data.map(({dates}) => ({...promotions, dates})); console.log(res);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.