JS converts nested objects to array of objects

I have it:

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 I 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 was something like this:

    var low_arr_dept = [];
    var medium_arr_dept = [];
    var high_arr_dept = [];

    for (var i in dataSet.data) {
                var department = departments_data[i];

                        for (var d in department) {
                            var score = department[d];
                            for (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 and then getting their length
            low_arr_dept_marketing.length.toString()
            medium_arr_dept_marketing.length.toString()
            high_arr_dept_marketing.length.toString()

// For 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},
          ...
    ];

      

+3


source to share


3 answers


You can use an array for groups and a hash table for 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; }
      

Run code


0


source


This is probably not the best way, although the code is pretty short, but I got what you need based on what you said you need. Check it.



var data = { 
data: {
    department: [
        { 
          "Marketing": [
            { "employee_1": "1210" },
            { "employee_2": "241" },
            { "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" }
          ]
        }
    ]
}
};

var new_data = data.data.department.map(function(el, ind) {
    var item = {low: 0, medium: 0, high: 0};
    // set the name of department for new array
    for(var prop in el) item.name = prop; 
    // go and check ids increasing counters
    for(var i = 0; i < el[prop].length; i++) for(var prop2 in el[prop][i]) if(el[prop][i][prop2] < 2000) item.low++; else if(el[prop][i][prop2] >= 5000) item.high++; else item.medium++;  return item;
});

console.log(new_data);
      

Run code


+1


source


Hello, here is my working solution for you.

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" }
              ]
            }
    	]
    }
    }
    
    var employeenumber = 1;
    var Low = 0;
    var Medium = 0;
    var High = 0;
    var data = [];
    var i = 0;
    for (var departments in dataSet.data) {
    	//console.log(JSON.stringify(dataSet.data[departments])); 
    	for (var arrays in dataSet.data[departments]) {
    		//console.log(dataSet.data[departments][arrays]);
    		var department = dataSet.data[departments][arrays];
    		for (var employees in department) {
    			//console.log(JSON.stringify(department[employees]));
    			var employeesArray = department[employees];
    			for (employee in employeesArray) {
    
    				var employeeScoreObject = employeesArray[employee];
    				var employee_score = employeeScoreObject["employee_"+employeenumber];
    				//console.log(employee_score);
    				if (employee_score < 2000) {
    					Low++;
    				}
    				if (employee_score >= 2000 && employee_score < 5000) {
    					Medium++;
    				}
    				if (employee_score > 5000) {
    					High++;
    				}
    				employeenumber++;
    				if (employeenumber>4) {
    				employeenumber = 1;
    				}
    			}
    			var departmentName = Object.keys(department)[0];
    
    			data[i] = {'name': departmentName, 'Low': Low, 'Medium': Medium, 'High': High};
    			i++;
    
    			Low = 0;
    			Medium = 0;
    			High = 0;
    		} // employees
    		
    	} // department 1= marketing 2 =....
    	
    } // departments
    console.log(JSON.stringify(data));
      

Run code


0


source







All Articles