Skip to content

Instantly share code, notes, and snippets.

@acjr1910
Created December 21, 2021 12:42
Show Gist options
  • Select an option

  • Save acjr1910/01e9e565ffe8151611c8c7cc57d9d431 to your computer and use it in GitHub Desktop.

Select an option

Save acjr1910/01e9e565ffe8151611c8c7cc57d9d431 to your computer and use it in GitHub Desktop.
sample
const obj = {
total_history: {
current_balance: 90,
funds: [
{
uuid: 'abd16f0a-9ae4-11ea-ad4d-0a334120f7c0',
name: 'Front RVT FIC FI Renda Fixa',
},
],
},
companies: {
company: {
uuid: '58ae9147-9ae4-11ea-ad4d-0a334120f7c0',
corporate_name: 'RED VENTURES SERVIÇOS DE MARKETING E TECNOLOGIA LTDA',
fantasy_name: 'Red Ventures',
},
histories: [
{
plan_uuid: 'b2c09c69-afbd-11ea-ad4d-0a334120f7c0',
contribution_type_slug: 'basic',
current_balance: 30,
},
{
plan_uuid: 'b2c09c69-afbd-11ea-ad4d-0a334120f7c0',
contribution_type_slug: 'company',
current_balance: 40,
},
{
plan_uuid: 'b2c09c69-afbd-11ea-ad4d-0a334120f7c0',
contribution_type_slug: 'extraordinary',
current_balance: 20,
},
],
},
plan_available: true,
};
const EXTRAORDINARY = 'extraordinary';
const BASIC = 'basic';
const mergeSlug = (histories) => {
const extraorinaryCurrentBalance = histories.find((h) => h.contribution_type_slug === EXTRAORDINARY).current_balance;
const merged = histories.reduce((acc, curr) => {
if (curr.contribution_type_slug === EXTRAORDINARY) return [...acc];
if (curr.contribution_type_slug === BASIC) return [...acc, { ...curr, current_balance: curr.current_balance + extraorinaryCurrentBalance }];
return [...acc, curr];
}, []);
return merged;
};
const parsedResponse = { ...obj, companies: { company: obj.companies.company, histories: mergeSlug(obj.companies.histories) } };
console.log(parsedResponse);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment