1

I have this data:

const data = [
  {
    name: 'chase',
    advisors: [
      {
        name: 'mark',
        clients: [
          { name: 'carol', savings: 500, checking: 600 },
          { name: 'toby', savings: 500, checking: 300 },
          { name: 'nich' }
        ]
      },
      {
        name: 'holly',
        clients: [
          { name: 'john', savings: 900 },
          { name: 'jim', checking: 200 },
          { name: 'bruce', checking: 200 },
          { name: 'sarah', savings: 500, checking: 300 }
        ]
      }
    ]
  },
  {
    name: 'citiBank',
    advisors: [
      {
        name: 'cindy',
        clients: [ { name: 'casey', savings: 500, checking: 200 } ]
      },
      { name: 'bob', clients: null }
    ]
  },
  { name: 'hsbc', advisors: null }
];

The output we have to get is an array of objects with that are ordered by greatest value of savings first, and if the savings value is the same, we have to order by the greatest checking value first.

Finally, the client array should look like this:

[{ name: 'john', savings: 900, firm:'chase',advisor:'holly' },{ name: 'carol', savings: 500, checking: 600, firm: 'chase', advisor: 'mark'},{ name: 'sarah', savings: 500, checking: 300 ,advisor:'holly',firm:'chase'},{ name: 'toby', savings: 500, checking: 300, firm:'chase',advisor:'mark', },{ name: 'casey', savings: 500, checking: 200,firm:'citi bank',advisor:'cindy' }....]

Below is the function defined

const maxSavingsData = ()=>{
  const client = [];
  console.log(client);
}
maxSavingsData(data);
4
  • What is the sort order when also checking is the same, like for Sarah and Toby? Commented Dec 1, 2022 at 19:27
  • it should be alphabetically based sarah should come first after that toby should come can you help me out by editing the answer Commented Jan 22, 2023 at 17:05
  • I have updated my answer to follow that requirement. It is a bit odd that it takes 7 weeks to answer a remark ;-) Commented Jan 22, 2023 at 19:26
  • @trincot i was suffering from a disease thats why i was completely out of it Anyway thanks a lot Commented Jan 30, 2023 at 5:24

1 Answer 1

1

I would split this task into two phases:

  1. First create the target objects in the order you get them from the source data;
  2. Sort that array using the sort callback function.

Here is how that could work:

const maxSavingsData = (data) => 
    data.flatMap(({name: firm, advisors}) =>
        (advisors ?? []).flatMap(({name: advisor, clients}) => 
            (clients ?? []).map(client => ({...client, firm, advisor}))
        )
    ).sort((a, b) => 
        (b.savings ?? 0) - (a.savings ?? 0) || 
        (b.checking ?? 0) - (a.checking ?? 0) ||
        a.name.localeCompare(b.name)
    );

const data = [{name: 'chase',advisors: [{name: 'mark',clients: [{ name: 'carol', savings: 500, checking: 600 },{ name: 'toby', savings: 500, checking: 300 },{ name: 'nich' }]},{name: 'holly',clients: [{ name: 'john', savings: 900 },{ name: 'jim', checking: 200 },{ name: 'bruce', checking: 200 },{ name: 'sarah', savings: 500, checking: 300 }]}]},{name: 'citiBank',advisors: [{name: 'cindy',clients: [ { name: 'casey', savings: 500, checking: 200 } ]},{ name: 'bob', clients: null }]},{ name: 'hsbc', advisors: null }];

const clients = maxSavingsData(data);
console.log(clients);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.