0

I am having the below JSON object.

"Department": [
      {
        "depType": "Testing",
        "name": "xyz",
        "address":""
      },
      {
        "deptype": "Developer",
        "name": "abc"
      }
    ]

I want to create another object based on the type of deptartment (depType). Something like this

"Testing":{
        "name": "xyz",
        "address":""
        },
"Developer":{
        "name": "abc"
        }

With the help of Object.keys, I was able to get the keys

7
  • 1
    It's not clear what is being asked? What problem are you struggling with? Commented Oct 17, 2014 at 8:19
  • @Tom Grant How to create a new object based on depType? Commented Oct 17, 2014 at 8:21
  • You want to check the depType on the first object, to then create another object containing what? Commented Oct 17, 2014 at 8:22
  • if depType = Testing then object should be formed lik this "Testing":{ "name": "xyz", "address":"" } Commented Oct 17, 2014 at 8:23
  • Problem definition not clear Commented Oct 17, 2014 at 8:27

1 Answer 1

1
  • You had some Property-naming issues with camelCased "depType". Fix that.
  • Create a new copy of the desired object to manipulate using JSON.parse(JSON.stringify(orgObj))
  • Loop that object to find the desired Property "Department"
  • Since Department is an Array of Objects you need to loop that Array for(var i=0; i<dep.length; i++).
  • Than you'll need to match if that Array contains arrObj.hasOwnProperty( "depType" )
  • if successful you can than fill your new object with all the info newObj[arrObj.depType] = arrObj;
  • Since now, inside your new object there's also the good old "depType" property you can get rid of it using delete.

jsBin demo

var myjson = {
  "Department": [
      {
        "depType": "Testing",   // NOTE: "camelCase"
        "name": "xyz",
        "address":""
      },
      {
        "depType": "Developer", // FIX: "camelCase" !!
        "name": "abc"
      }
    ]
};

function depTypify( orgObj ) {
  var objCopy = JSON.parse(JSON.stringify(orgObj)); // Fresh copy
  var newObj = {};
  for(var prop in objCopy){
    if(prop === "Department") {
      var dep = objCopy[prop];          // get Department Array
      for(var i=0; i<dep.length; i++) { // Loop array
        var arrObj = dep[i];            // Explore Each Array Object
        if(arrObj.hasOwnProperty( "depType" )) {
          newObj[arrObj.depType] = arrObj;
          delete arrObj.depType;        // We don't need it any more
        }
      }
    }
  }
  return newObj;
}

var myNewJson = depTypify( myjson );

if you do than console.log( myNewJson ) this is what you'll get:

[object Object] {
  Developer: [object Object] {
    name: "abc"
  },
  Testing: [object Object] {
    address: "",
    name: "xyz"
  }
}

The nice thing is that your old json is still intact.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.