I have an object like this, that contains a lot more data than what is present but this is just a small sample.
I am iterating through the object and checking if EmployeeLocations = object. If it does, I would like to add that to a new array along with the nested value. For example, "EmployeeLocationA : FL HQ" would be added to the array, "EmployeeLocationB" : "New York", "DoorsA" : 345, would be added and so forth.
This object will be joined with the values of the original object and returned.
{
"Building": 4,
"Doors":{
"a": 345,
"b": 87,
"c": 98
}
"EmployeeId": 565,
"EmployeeLocation": {
"a": "FL HQ",
"b": "New York",
"c": "Chicago"
},
I am able to iterate and get the following information, but I am unable to add it to the array. What I am trying to get back is an array that looks like this:
"Building": 4, "DoorsA": 345, "DoorB": 87, "DoorsC": 98, "EmployeeId": 565, "EmployeeLocationA": "FL HQ", "EmployeeLocationB": "New York", "EmployeeLocationC": "Chicago" ,
Object.keys(Details).forEach((key, value) => {
if(typeof( Details[key]) === 'object' ){
const objPositions = Details[key]; // holds inner values
console.log("objposition " + objPositions[key] + allDetails[key])
console.log("---->" + key + "X " + objPositions.x);
console.log("---->" + key + "Y " + objPositions.y);
console.log("---->" + key + "Z " + objPositions.z);
let xKey = key + "X"
arr.push(key + "X" , objPositions.x)
// arr.push(xKey.toString() : objPositions.x.toString())
I am really new at javascript and am having big trouble with it. Can someone shed a little light? Thanks
3
Answers
Edit: a more dynamic solution that flattens it dynamically:
Not sure why you are using
Object.keys
to loop over your object.But to give you a general idea, this should give you the expected outcome:
I think that you want to flatten an object
Your solution: