javascript - JS converting nested objects into an array of objects -
i have this:
var dataset = { data: { department: [ { "marketing": [ { "employee_1": "1210" }, { "employee_2": "3241" }, { "employee_3": "6510" }, { "employee_4": "9510" } ] }, { "sales": [ { "employee_1": "5210" }, { "employee_2": "1241" }, { "employee_3": "8510" }, { "employee_4": "2510" } ] }, { "rd": [ { "employee_1": "1010" }, { "employee_2": "9241" }, { "employee_3": "9430" }, { "employee_4": "3510" } ] }, ... ] }
}
and need this:
const data = [ {name: 'marketing', low: 2, medium: 4, high: 3}, {name: 'sales', low: 3, medium: 1, high: 5}, {name: 'rd', low: 6, medium: 1, high: 2}, ... ];
where:
low employee_x < 2000 medium employee_x >= 2000 && employee_x < 5000 high employee_x >= 5000
my last attempt this:
var low_arr_dept = []; var medium_arr_dept = []; var high_arr_dept = []; (var in dataset.data) { var department = departments_data[i]; (var d in department) { var score = department[d]; (var j in score) { if (object.values(department[d][j]) >= 5000) { console.log("object.values(department[d][j]) ", object.values(department[d][j])) low_arr_dept.push({ score: object.values(department[d][j]) }); } else if (object.values(department[d][j]) >= 2000 && object.values(department[d][j]) < 5000) { medium_arr_dept.push({ score: object.values(department[d][j]) }); } else if (object.values(department[d][j]) <2000) { high_arr_dept.push({ score: object.values(department[d][j]) }); } } } } // creating arrays , getting length low_arr_dept_marketing.length.tostring() medium_arr_dept_marketing.length.tostring() high_arr_dept_marketing.length.tostring() // creating this: const data = [ {name: 'marketing', low: low_arr_dept_marketing.length, medium: medium_arr_dept_marketing.length, high: high_arr_dept_marketing.length}, {name: 'sales', low: low_arr_dept_sales.length, medium: medium_arr_dept_sales.length, high: high_arr_dept_sales.length}, {name: 'rd'low: low_arr_dept_rd.length, medium: medium_arr_dept_rd.length, high: high_arr_dept_rd.length}, ... ];
you use array groups , hash table departments.
var dataset = { data: { department: [{ marketing: [{ employee_1: "1210" }, { employee_2: "3241" }, { employee_3: "6510" }, { employee_4: "9510" }] }, { sales: [{ employee_1: "5210" }, { employee_2: "1241" }, { employee_3: "8510" }, { employee_4: "2510" }] }, { rd: [{ employee_1: "1010" }, { employee_2: "9241" }, { employee_3: "9430" }, { employee_4: "3510" }] }] } }, groups = [{ key: 'low', value: 2000 }, { key: 'medium', value: 5000 }, { key: 'high', value: infinity }], hash = {}, result = []; dataset.data.department.foreach(function (o) { var dept = object.keys(o)[0]; o[dept].foreach(function (p) { var value = p[object.keys(p)[0]], group = groups.find(g => +value < g.value).key; if (!hash[dept]) { hash[dept] = { name: dept }; groups.foreach(g => hash[dept][g.key] = 0); result.push(hash[dept]); } hash[dept][group]++; }); }); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Comments
Post a Comment