使用托儿所-我需要在两个数组的两个列上匹配两个数组:
部门出勤:[日期,部门,出席情况]
0: (3) ["09-30-2019", "00_Infants", 22]
1: (3) ["09-30-2019", "01_Ones", 38]
2: (3) ["09-30-2019", "02_Twos", 40]
3: (3) ["09-30-2019", "03_Threes", 42]
4: (3) ["09-30-2019", "04_Fours", 19]
5: (3) ["10-01-2019", "00_Infants", 27]
6: (3) ["10-01-2019", "01_Ones", 42]
7: (3) ["10-01-2019", "02_Twos", 51]
ETC ...
部门费用[日期,部门,费用]
0: (3) ["09-30-2019", "00_Infants", "584.56"]
1: (3) ["09-30-2019", "01_Ones", "701.51"]
2: (3) ["09-30-2019", "02_Twos", "614.02"]
3: (3) ["09-30-2019", "03_Threes", "442.50"]
4: (3) ["09-30-2019", "04_Fours", "166.65"]
5: (3) ["09-30-2019", "06_Floater", "141.37"]
6: (3) ["09-30-2019", "07_Office", "246.91"]
7: (3) ["09-30-2019", "08_Administration", "0.00"]
8: (3) ["09-30-2019", "09_Director", "0.00"]
9: (3) ["09-30-2019", "12_Kitchen", "0.00"]
10: (3) ["10-01-2019", "00_Infants", "760.37"]
11: (3) ["10-01-2019", "01_Ones", "756.48"]
12: (3) ["10-01-2019", "02_Twos", "640.23"]
13: (3) ["10-01-2019", "03_Threes", "552.66"]
-
我只想将.department出现在出勤情况中,将Expense.date && Expense.department与Attendance.date && Attendance.department进行匹配 然后, 将Expense.expense附加到出勤中的匹配记录中
我尝试过映射,过滤,d3.js nest(),Object.assign(),ifs,$。each ...
这是我还没有删除并从头开始的最后一件非常糟糕的事情-我知道仅此一项看起来就很糟糕,而且还没有结束;但是,需要的代码。
let ffs = attended.map(function (x, i) {
return {
"date": emp[x],
"other": emp[i][0]
}
}.bind(this));
let mfer = attended.map(x => Object.assign(x, emp.find(y => y[0] === x[0])));
预期结果:[日期,部门,出席情况,费用]
0: (3) ["09-30-2019", "00_Infants", 22, 584.56]
1: (3) ["09-30-2019", "01_Ones", 38, 701.51]
2: (3) ["09-30-2019", "02_Twos", 40, 613.02]
我认为这应该是简单的解决方案,但是我处于学习状态,我所面临的挑战超出了我应该花的时间。
- 我认为这与我需要的最接近,但是当尝试将第二个过滤器应用于捕获部门时,一切都变得不清楚:
一些打开的标签...
Find all matching elements with in an array of objects
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
答案 0 :(得分:1)
您可以通过“键”将Map
中的这些元素分组。在您的情况下,键可以是日期和部门。所以
const attendance = [
["09-30-2019", "00_Infants", 22],
["09-30-2019", "01_Ones", 38],
["09-30-2019", "02_Twos", 40],
["09-30-2019", "03_Threes", 42],
["09-30-2019", "04_Fours", 19],
["10-01-2019", "00_Infants", 27],
["10-01-2019", "01_Ones", 42],
["10-01-2019", "02_Twos", 51]
];
const expenses = [
["09-30-2019", "00_Infants", "584.56"],
["09-30-2019", "01_Ones", "701.51"],
["09-30-2019", "02_Twos", "614.02"],
["09-30-2019", "03_Threes", "442.50"],
["09-30-2019", "04_Fours", "166.65"],
["09-30-2019", "06_Floater", "141.37"],
["09-30-2019", "07_Office", "246.91"],
["09-30-2019", "08_Administration", "0.00"],
["09-30-2019", "09_Director", "0.00"],
["09-30-2019", "12_Kitchen", "0.00"],
["10-01-2019", "00_Infants", "760.37"],
["10-01-2019", "01_Ones", "756.48"],
["10-01-2019", "02_Twos", "640.23"],
["10-01-2019", "03_Threes", "552.66"]
];
const key = (date, department) => `${date};${department}`;
const expenseMap = new Map(expenses.map(([date, dept, expense]) => [key(date, dept), expense]));
attendance.forEach(a => a.push(expenseMap.get(key(a[0], a[1]))));
console.log(attendance)
答案 1 :(得分:1)
遍历Attendance
,并用Array.prototype.find()
检查Departments
中是否有匹配项。如果找到匹配的部门,我们会将(解析的)金额推送到出勤条目中。
const attendance = [["09-30-2019", "00_Infants", 22],["09-30-2019", "01_Ones", 38],["09-30-2019", "02_Twos", 40],["09-30-2019", "03_Threes", 42],["09-30-2019", "04_Fours", 19],["10-01-2019", "00_Infants", 27],["10-01-2019", "01_Ones", 42],["10-01-2019", "02_Twos", 51]];
const departments = [["09-30-2019", "00_Infants", "584.56"],["09-30-2019", "01_Ones", "701.51"],["09-30-2019", "02_Twos", "614.02"],["09-30-2019", "03_Threes", "442.50"],["09-30-2019", "04_Fours", "166.65"],["09-30-2019", "06_Floater", "141.37"],["09-30-2019", "07_Office", "246.91"],["09-30-2019", "08_Administration", "0.00"],["09-30-2019", "09_Director", "0.00"],["09-30-2019", "12_Kitchen", "0.00"],["10-01-2019", "00_Infants", "760.37"],["10-01-2019", "01_Ones", "756.48"],["10-01-2019", "02_Twos", "640.23"],["10-01-2019", "03_Threes", "552.66"]];
attendance.forEach(a => {
const department = departments.find(d => a[0] === d[0] && a[1] === d[1]);
if (department) {
a.push(parseFloat(department[2]));
}
});
console.log(attendance);
答案 2 :(得分:1)
您可以在出勤阵列上使用map(),并使用find()查找该日期和部门的费用。
如果存在费用,则追加费用,否则返回相同的项目。
var attendance = [ ["09-30-2019", "00_Infants", 22], ["09-30-2019", "01_Ones", 38], ["09-30-2019", "02_Twos", 40], ["09-30-2019", "03_Threes", 42], ["09-30-2019", "04_Fours", 19], ["10-01-2019", "00_Infants", 27], ["10-01-2019", "01_Ones", 42], ["10-01-2019", "02_Twos", 51] ];
var expenses = [ ["09-30-2019", "00_Infants", "584.56"], ["09-30-2019", "01_Ones", "701.51"], ["09-30-2019", "02_Twos", "614.02"], ["09-30-2019", "03_Threes", "442.50"], ["09-30-2019", "04_Fours", "166.65"], ["09-30-2019", "06_Floater", "141.37"], ["09-30-2019", "07_Office", "246.91"], ["09-30-2019", "08_Administration", "0.00"], ["09-30-2019", "09_Director", "0.00"], ["09-30-2019", "12_Kitchen", "0.00"], ["10-01-2019", "00_Infants", "760.37"], ["10-01-2019", "01_Ones", "756.48"], ["10-01-2019", "02_Twos", "640.23"], ["10-01-2019", "03_Threes", "552.66"] ];
var result = attendance.map(item => {
let expense = expenses.find(expense => expense[0] === item[0] && expense[1] === item[1]);
if (expense) {
return [...item, expense[2]];
} else {
return item;
}
});
console.log(result);