javascript - 过滤合并两个数组,匹配两个字段/列 : Javascript

标签 javascript arrays json d3.js

使用托儿学校 - 我有两个数组,我需要在每个数组的 2 列上匹配:

按部门出勤:[日期、部门、出席]

    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]

等...

按部门划分的费用[日期、部门、费用]

    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"]

--

我只想将 Expense.date && Expense.department 与 Attendance.date && Attendance.department 匹配,仅当 .department 出现在 Attendance[] 中时 然后, 将 Expense.expense 附加到 Attendance 中的匹配记录

我尝试过映射、过滤、d3.js nest()、Object.assign()、ifs、$.each...

这是我没有删除并从头开始的最后一些非常糟糕的东西——我知道它看起来很糟糕而且不接近;然而,SO 想要代码。

    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]

实际结果:除了。

我的浏览器搜索历史显示从昨天 19:00 到现在 08:30 有 115 个相关查询。

我相信这应该是一个简单的解决方案,但我处于学习状态,我面临的挑战超出了我应该花在它上面的时间。

- 我相信这是最接近我需要的,但是当尝试将第二个过滤器应用于捕获部门时,一切都变得不清楚:

Applying filter within a filter in JavaScript

一些打开的标签...

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

Javascript multiple filters array

merge two arrays including some fields from each

最佳答案

迭代 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);

关于javascript - 过滤合并两个数组,匹配两个字段/列 : Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58398544/

相关文章:

javascript - 试图通过使用 d3 或任何其他方式找到一种在 html/js 中获取 csv 的行名和列名的方法?

javascript - 就地修改对象 javascript

javascript - 将数组分割成 block

json - 使用单参数案例类进行 JSON 读/写

java - 尝试传递 customAdapter 时出现空指针异常?

javascript - 是否可以使用 JavaScript 操作嵌入在 HTML 文档中的 SVG 文档?

python - numpy 中用最少内存对上三角元素求和的最快方法

c - 对数组执行操作

python - 如果键包含字符串,则删除整个 json 对象

javascript - 不更新分数属性。 (压倒性的分数)