I have an nested object like following(it can have any depth):

```
{
    "a": 5,
    "b": {
        "0": 1,
        "1": "x"
    },
    "x": {
        "a": 1,
        "1": "z"
    },
    "c": {
        "0": 3,
        "1": "Am",
        "3":{
            "0": 3,
            "1": "x",
            "2":{
                "0": 3,
                "1": "Y"
            },
            "length": 3
        },
        "length": 4
    }
}

```

I have to convert it as following(object may or may not have length property):

```
{
    "a": 5,
    "x": {
        "a": 1,
        "1": "z"
    },
    "b": [1,"x"],
    "c": [3, "Am",undefind, [ 3, "x", [ 3, "Y" ] ] ]
}

```

I written method like following:

```
function formatObjToArr(obj) {
    var kys = Object.keys(obj);
    var isObj = (/[^0-9]/g).test(kys.join('')) && !Array.isArray(obj);
    if(!isObj){
        obj.length===undefined && (obj.length = Math.max.apply(null,kys.map(function (i) { return parseInt(i) }))+1);
        obj = Array.prototype.map.apply(obj,[function (i) { return i}]);
        obj.forEach(function (i) {
            (typeof i === "object" && i!==null) && formetObjToArr(i);
        })
    }else {
        for (var property in obj) {
            if (obj.hasOwnProperty(property) && property!=='length') {
                if (typeof obj[property] === "object" && obj[property]!==null) {
                    formetObjToArr(obj[property]);
                }
            }
        }
    }
}

```

But it just adds the length property it is not changing the type. output of my code is as follows:

```
{
  "a": 5,
  "b": {
      "0": 1,
      "1": "x",
      "length": 2
   },
  "x": {
      "a": 1,
      "1": "z"
  },
  "c": {
      "0": 3,
      "1": "Am",
      "3": {
          "0": 3,
          "1": "x",
          "2": {
              "0": 3,
              "1": "Y",
              "length": 2
          },
          "length": 3
      },
      "length": 4
  }
}

```