[英]Creating arrays in JavaScript by comparing values in another array
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) eg 我有一个字符串格式的日期数组(日期),例如
["30/09/2015", "31/10/2015", "30/11/2015", "31/12/2015"]
I've got an Object to represent multiple bank accounts (balancesSortByAccId) eg 我有一个对象来表示多个银行帐户(balanceSortByAccId),例如
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 ie 在循环浏览balancesSortByAccId中的所有帐户时,我希望能够在dates数组中的每个日期为余额创建一个数组,即
[30, 50, 100, null]
[null, null, 400, 200]
How could I achieve this? 我怎样才能做到这一点?
UPDATE: jsfiddle code - https://jsfiddle.net/gx8bLehb/ UPDATE:的jsfiddle码- https://jsfiddle.net/gx8bLehb/
The easiest way would be to transform your cash
and natwest
arrays into a hash sorted by date, something like balancesByDate
: 最简单的方法是将
cash
和natwest
数组转换为按日期排序的哈希,例如balancesByDate
:
var balancesByDate = _.groupBy(cash, function(entry) {return entry.date});
Then use an array map()
function, eg from lodash to iterate the dates
array and for each date look up the account line in the balancesByDate
hash. 然后使用数组
map()
函数,例如从lodash迭代的dates
阵列和用于每个日期查找在该帐户线balancesByDate
散列。 From that line, return the balance
property in the map
function. 从那一行,返回
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/
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>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.