通过比较另一个数组中的值来创建JavaScript数组。

时间:2022-01-27 16:32:28

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

在JavaScript中,我尝试基于另一个数组的值创建数组,但我遇到了困难。

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.

我有一个对象来表示多个银行账户(balancesSortByAccId)。

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.

当循环遍历balancesSortByAccId中的所有帐户时,我希望能够为date数组中每个日期的余额创建一个数组。

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

How could I achieve this?

我怎么能做到这一点呢?

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

更新:jsfiddle代码—https://jsfiddle.net/gx8bLehb/

2 个解决方案

#1


3  

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

最简单的方法是将您的现金和natwest数组转换为按日期排序的散列,类似于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.

然后使用数组映射()函数,例如,从lodash开始迭代日期数组,并在balancesByDate散列中查找每个日期的帐户行。从该行返回map函数中的balance属性。

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/

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

#2


1  

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

一个简单的javascript解决方案,包含日期的帮助对象:

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>');

#1


3  

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

最简单的方法是将您的现金和natwest数组转换为按日期排序的散列,类似于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.

然后使用数组映射()函数,例如,从lodash开始迭代日期数组,并在balancesByDate散列中查找每个日期的帐户行。从该行返回map函数中的balance属性。

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/

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

#2


1  

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

一个简单的javascript解决方案,包含日期的帮助对象:

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>');