2

This is my JSON Object in this i want to remove "item" key from json and want to keep its inner object include with "children" and its tree like JOSN. How can I do this?

[
    {
      "item": {
        "id": 11865,
        "parentid": null,
        "levelid": 63,
        "name": "Total"
      },
      "children": [
        {
          "item": {
            "id": 10143,
            "parentid": 11865,
            "levelid": 19,
            "name": "Productive"
          }
        }
      ]
    }
  ]
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
code Zero
  • 115
  • 2
  • 8

4 Answers4

0

try

objArray = objArray.map( function(value){ 
   var item = value.item; 
   for( var key in item ) 
   { 
     value[key] = item[key]; 
   } 
   delete value.item; 
   return value;
});

DEMO

Explanation

1) Use map to iterate on each item (value) of this given array objArray.

2) Get the item property of value, assign them to value directly

3) Finally delete the item property of the value.

Faster option

objArray = objArray.map( function(value){ 
   var item = value.item; 
   var itemKeys = Object.keys(item); 
   for( var counter = 0; counter < itemKeys.length; counter++ ) 
   { 
     value[itemKeys[counter]] = item[itemKeys[counter]]; 
   } 
   delete value.item; 
   return value;
});
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • will for-in query properties in the parent prototype ? Object.keys() better off from an efficiency stand point ? – trk Mar 11 '16 at 11:01
  • @82Tuskers Not sure I see a huge benefit in this case since number of properties are quite small. Will update the answer with another options anyways since `.keys` seems to be faster http://jsperf.com/object-keys-foreach-vs-for-in-hasownproperty/8 – gurvinder372 Mar 11 '16 at 11:05
  • yeah .. the problem is not so much in the user object but the host object. The user object will have that in its prototype chain. – trk Mar 11 '16 at 11:09
0

If I'm understanding what you want your object to look like after correctly, then this should do the trick:

var arrayOfObjects = [
    {
      "item": {
        "id": 11865,
        "parentid": null,
        "levelid": 63,
        "name": "Total"
      },
      "children": [
        {
          "item": {
            "id": 10143,
            "parentid": 11865,
            "levelid": 19,
            "name": "Productive"
          }
        }
      ]
    }
]
arrayOfObjects.forEach(function(obj) {
  obj.id = obj.item.id;
  obj.parentid = obj.item.parentid;
  obj.levelid = obj.item.levelid;
  obj.name = obj.item.name;
  delete obj.item;
});

All this is doing is manually moving the data from obj.item to obj and then deleting obj.item entirely.

millerbr
  • 2,951
  • 1
  • 14
  • 25
0

You can use a recursion, which keeps the content of item and adds the children property as well.

function delItem(a, i, aa) {
    var children = a.children;
    if (a.item) {
        aa[i] = a.item;
        aa[i].children = children;
        delete a.item;
    }
    Array.isArray(children) && children.forEach(delItem);
}

var array = [{ "item": { "id": 11865, "parentid": null, "levelid": 63, "name": "Total" }, "children": [{ "item": { "id": 10143, "parentid": 11865, "levelid": 19, "name": "Productive" } }] }];
array.forEach(delItem);
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

I would do this:

//your original array of objects
var array = [{
  "item": {
    "id": 11865,
    "parentid": null,
    "levelid": 63,
    "name": "Total"
  },
  "children": [
    {
      "item": {
        "id": 10143,
        "parentid": 11865,
        "levelid": 19,
        "name": "Productive"
      }
    }
  ]
}, ...];

array.forEach(function(parent) {
    flattenKey(parent, 'item');
});

function flattenKey(parent, keyName) {
    var section = parent[keyName];
    var child = section ? section : {};
    var keys = Object.keys(child);
    keys.forEach(function(key) {
        parent[key] = child[key];
    })
    delete parent[keyName];
}

basically, the function flattenKey would flatten any key for a given object (given its key).

  1. logic is similar to other solutions here: iterate through child keys and assign their values to the parent object (flattening).
  2. then it deletes the child key after step 1.
trk
  • 2,106
  • 14
  • 20