如何映射/合并结构不同的对象

时间:2019-10-25 06:48:57

标签: javascript arrays json d3.js

摘要:

我需要将Students中的值插入相应的Employees中,以使Students值与Employees['avg_rate' and 'expense']出现在同一数组中;

新的或修改的对象数组应在日期和部门(例如00_Infants)上匹配,然后排除学生和员工中都没有的部门。

谢谢。


// "Students" aka "attendance values by department"
Object
    0:
        00_Infants: 30
        01_Ones: 35
        02_Twos: 39
        03_Threes: 38
        04_Fours: 21
        05_School_Age: 0
        key: "10-10-2019"
        __proto__: Object
    1: {key: "10-11-2019", 00_Infants: 33, 01_Ones: 35, 02_Twos: 36, …}
    2: {key: "10-14-2019", 00_Infants: 30, 01_Ones: 33, 02_Twos: 40, …}

// "employees"
Array(12)
    0:
        key: "10-10-2019"
        values: Array(11)
            0:
                key: "00_Infants"
                value:
                    avg_rate: 13.236842105263158
                    expense: 764.1350000000001
                    __proto__: Object
                    __proto__: Object
            1: {key: "01_Ones", value: {…}}
            2: {key: "02_Twos", value: {…}}
            3: {key: "03_Threes", value: {…}}
            4: {key: "04_Fours", value: {…}}
            5: {key: "06_Floater", value: {…}}
            6: {key: "07_Office", value: {…}}
            7: {key: "08_Administration", value: {…}}
            8: {key: "09_Director", value: {…}}
            9: {key: "12_Kitchen", value: {…}}
            10: {key: "13_Unknown", value: {…}}
            length: 11
            __proto__: Array(0)
            __proto__: Object
    1: {key: "10-11-2019", values: Array(10)}
    2: {key: "10-14-2019", values: Array(9)}

所需结果:

{
    Date:"10-09-2019"{
        00_Infants: {
            avg_rate:     13.39,
            expense:     776.58,
            attendance:   28
        },
        01_Ones: {
            avg_rate:     13.31,
            expense:     716.58,
            attendance:   33
        },
},        
{   // for each date
    Date:"10-10-2019"{ 
    // for each department ^
    }
}

1 个答案:

答案 0 :(得分:1)

  • Object.values()可用于获取function newFile() { var number = '1'; var newFile = SpreadsheetApp.create("Roll " + number); } 对象的值,而forEach()可用于迭代这些值。
  • find()方法可用于在students数组中查找具有匹配日期的员工。
  • 然后针对学生对象项中的每个值,employees可用于查找员工对象中的部门,并可以添加具有适当值的find()属性。

代码:

attendance