1

I'm trying to optimize my code or see if there's a better way to do it. Given values for acctData and balances below, I need to return an array of accounts filtered by the user name. my code works

const acctData = [{
    acctNum: "AAA - 1234",
    user: "Alice"
  },
  {
    acctNum: "AAA - 5231",
    user: "Bob"
  },
  {
    acctNum: "AAA - 9921",
    user: "Alice"
  },
  {
    acctNum: "AAA - 8191",
    user: "Alice"
  }
];

const balance = {
  "AAA - 1234": 4593.22,
  "AAA - 9921": 0,
  "AAA - 5231": 232142.5,
  "AAA - 8191": 4344
};

const combinedAccBalance = JSON.parse(JSON.stringify(acctData));

const getAccountNumbers = (filterByUser) => {
  return combinedAccBalance
    .filter(acc => {
      acc.balance = balance[acc.acctNum];
      return acc.user === filterByUser;
    })
    .sort((a, b) => {
      return a.balance - b.balance;
    })
    .map(fa => {
      return fa.acctNum;
    });
};

console.log(getAccountNumbers("Alice"));
//Returns ["AAA - 9921", "AAA - 8191", "AAA - 1234"]

3
  • 2
    Consider posting this on Code Review since it works and you're asking for improvements. Commented Mar 19, 2020 at 6:15
  • 2
    This looks perfectly reasonable to me. But I don't think you need the deep copy action of const combinedAccBalance = JSON.parse(JSON.stringify(acctData)); as you are not changing anything in the array in getAccountNumbers(). The .filter() method will provide a shallow array copy, which is then sorted. So, no change to the original array. Commented Mar 19, 2020 at 6:23
  • Hi ! @cars10m I'm adding a new property acc.balance = balance[acc.acctNum]; where I'm filtering, so it does change the original array. Thoughts? Commented Mar 19, 2020 at 6:27

1 Answer 1

3

you can just skip the deep clone part and do something like this:

const acctData = [{
    acctNum: "AAA - 1234",
    user: "Alice"
  },
  {
    acctNum: "AAA - 5231",
    user: "Bob"
  },
  {
    acctNum: "AAA - 9921",
    user: "Alice"
  },
  {
    acctNum: "AAA - 8191",
    user: "Alice"
  }
];

const balance = {
  "AAA - 1234": 4593.22,
  "AAA - 9921": 0,
  "AAA - 5231": 232142.5,
  "AAA - 8191": 4344
};

let getAccountNumbers = (filterByUser) => {
  return acctData
    .filter(acc => acc.user === filterByUser)
    .sort((a, b) => {
      return balance[a.acctNum] - balance[b.acctNum];
    })
    .map(fa => {
      return fa.acctNum;
    });
};

console.log(getAccountNumbers("Alice"));
//Returns ["AAA - 9921", "AAA - 8191", "AAA - 1234"]

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

3 Comments

ahhhhhh niceeee! Thanks!! I like that I don't have to make the copy of the array with this solution
yeah with this basically you could just avoid the whole copying thing and get the same result
removing the {} and return statements (like the filter), and doing the sort after the map will shorten it even more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.