skip to Main Content

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


  1. Edit: a more dynamic solution that flattens it dynamically:

    const data = {
        "Building": 4,
        "Doors": { "a": 345, "b": 87, "c": 98 },
        "EmployeeId": 565,
        "EmployeeLocation": {
            "a": "FL HQ",
            "b": "New York",
            "c": "Chicago"
        }
    };
    
    const result = Object.keys(data).reduce((p, c) => {
        if (typeof data[c] === 'object') {
            const tmp = Object.keys(data[c]).reduce((x, y) => ({ ...x, [c + y.toUpperCase()]: data[c][y] }), {});
            return { ...p, ...tmp };
        }
        return { ...p, [c]: data[c] }; 
    }, {});
    
    console.log(result);

    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:

    const data = {
        "Building": 4,
        "Doors": { "a": 345, "b": 87, "c": 98 },
        "EmployeeId": 565,
        "EmployeeLocation": {
            "a": "FL HQ",
            "b": "New York",
            "c": "Chicago"
        }
    }
    
    if (typeof data.EmployeeLocation === 'object') {
        Object.keys(data.EmployeeLocation).forEach(k => {
            data['EmployeeLocation' + k.toUpperCase()] = data.EmployeeLocation[k];
        });
    }
      
    console.log(data);
    Login or Signup to reply.
  2. I think that you want to flatten an object

    const obj = {
      Building: 4,
      Doors: {
        a: 345,
        b: 87,
        c: 98,
      },
      EmployeeId: 565,
      EmployeeLocation: {
        a: "FL HQ",
        b: "New York",
        c: "Chicago",
      },
    };
    
    const newObject = flatten(obj);
    
    console.log(newObject);
    
    function flatten(obj, prefix) {
      const newObject = {};
      Object.entries(obj).forEach(([key, value]) => {
        if (typeof value === "object") {
          Object.assign(newObject, flatten(value, key));
        } else {
          newObject[prefix ? prefix + key.toUpperCase() : key] = value;
        }
      });
      return newObject;
    }
    Login or Signup to reply.
  3. Your solution:

    const obj = {
      "Building": 4,
      "Doors":{
       "a": 345,
       "b": 87,
       "c": 98
      },
      "EmployeeId": 565,
      "EmployeeLocation": {
        "a": "FL HQ",
        "b": "New York",
        "c": "Chicago"
      },
    }
    
    const solObj = {}
    
    for(const key in obj){
        if(typeof obj[key] === "object"){
            const childObj = obj[key];
            for(const childKey in childObj){
                solObj[key+childKey]= childObj[childKey];
            }
        }else{
            solObj[key]=obj[key];
        }
    }
    console.log(solObj)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search