2

In JavaScript, I'm trying to create arrays based on the values of another array and I'm having difficultly.

I've got an array of dates in string format (dates) e.g.

["30/09/2015", "31/10/2015", "30/11/2015", "31/12/2015"]

I've got an Object to represent multiple bank accounts (balancesSortByAccId) e.g.

Cash - (Array size: 3)
id: 1, date: "30/09/2015", balance: 30
id: 2, date: "31/10/2015", balance: 50
id: 3, date: "30/11/2015", balance: 100

Natwest - (Array size: 2)
id: 4, date: "30/11/2015", balance: 400
id: 5, date: "31/12/2015", balance: 200

Whilst looping through all the accounts in balancesSortByAccId, I want to be able to create an array for the balance at each date in the dates array i.e.

[30, 50, 100, null]
[null, null, 400, 200]

How could I achieve this?

UPDATE: jsfiddle code - https://jsfiddle.net/gx8bLehb/

6
  • can you give us a fiddle with appropriate array values? Commented Jan 29, 2016 at 17:58
  • Please format the strings in your code as actual strings (e.g. with quotes around them). Commented Jan 29, 2016 at 18:13
  • @jfriend00 Have done. Commented Jan 29, 2016 at 18:25
  • Hey, did any of the answers solve your problem? If yes, please accept it. Commented Jan 30, 2016 at 8:15
  • @JohannesJander I am still attempting to resolve this. I understand that my original question/jsfiddle code isn't clear and so I was wondering whether I should delete this question and re-post again or just modify the OP? Commented Jan 30, 2016 at 10:15

2 Answers 2

3

The easiest way would be to transform your cash and natwest arrays into a hash sorted by date, something like balancesByDate:

    var balancesByDate = _.groupBy(cash, function(entry) {return entry.date});

Then use an array map() function, e.g. from lodash to iterate the dates array and for each date look up the account line in the balancesByDate hash. From that line, return the balance property in the map function.

dates.forEach(function(date){
  if (balancesByDate[date]) {
    results.push(_.map(balancesByDate[date], function(line){
       return line.balance;
    }));
  } else {
    results.push(null);
  }
});

However, you need to be aware that your dataset most likely could contain duplicate balances for a day, you should plan for that (my code returns an array for each day).

https://jsfiddle.net/hdpuuc5d/1/

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

4 Comments

Or, you know, the built-in Array.prototype.map instead of importing an extra library... ;)
@MikeMcCaughan: yes, depending on the browser version OP needs to support.
@JohannesJander How can I transform balancesSortByAccId into a hash as you mentioned? I've added a link to jsfiddle with sample data/my code in the OP.
I have amended my answer in response to the fiddle. Unfortunately, in the fiddle, the data structure is somewhat different than in the question, that's not really nice. Also there were bugs and undeclared variables in the fiddle - please if you want help from people, do check and correct your stuff. Anyhow, here's a working example: jsfiddle.net/hdpuuc5d/1 - please be aware that you can have more than one balance result for a given date - therefore the results-array contains an array for each day.
1

A solution in plain javascript with a helper object for the dates:

var dates = ["30/09/2015", "31/10/2015", "30/11/2015", "31/12/2015"],
    datesObj = dates.reduce(function (r, a, i) { r[a] = i; return r; }, {}),
    balances = {
        Cash: [
            { id: 1, date: "30/09/2015", balance: 30 },
            { id: 2, date: "31/10/2015", balance: 50 },
            { id: 3, date: "30/11/2015", balance: 100 }
        ],
        Natwest: [
            { id: 4, date: "30/11/2015", balance: 400 },
            { id: 5, date: "31/12/2015", balance: 200 }
        ]
    },
    result = {};

Object.keys(balances).forEach(function (k) {
    result[k] = Array.apply(null, { length: dates.length }).map(function () { return null; });
    balances[k].forEach(function (a) {
        result[k][datesObj[a.date]] = a.balance;
    });
});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Comments

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.