This objTest is an array of objects. To remove depth properties in a precise object from a recursive function, that is: to call itself to each sub-level to object.
I created one that verifies if a property is a sub-level (another object) and if not, it checks to see if the property is id
. If it is to be deleted.
function recursiva(obj) {
for (var k in obj) {
if (typeof obj[k] == "object" && obj.hasOwnProperty(k)) recursiva(obj[k]);
else if (k == 'id') delete obj.id;
}
}
objTeste.forEach(recursiva);
jsFiddle: link
In this case you do not even need to use .map
because once you change the object inside the array it changes directly in the original.
If you want to create a new array, with new objects (without the ID) you can do this:
function recursiva(obj) {
var _obj = {};
for (var k in obj) {
if (typeof obj[k] == "object" && obj.hasOwnProperty(k)) _obj[k] = recursiva(obj[k]);
else if (k != 'id') _obj[k] = obj[k];
}
return _obj;
}
var novo = objTeste.map(recursiva);
console.log(JSON.stringify(novo)); // [{"nome":"teste03","pai":{"nome":"teste02","pai":{"nome":"teste01"}}}]
jsFiddle: link