This objTeste is an array of objects. To remove depth properties on a precise object from a recursive function, that is, to call itself at each sub-level to the object.
I created one that checks if a property is a sublevel (other object) and if it is not checked if the property is id
. In case it’s 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);
In this case you don’t even need to use the .map
for once changing the object within the array it changes directly in the original.
If you want to create a new array with new objects (without the ID):
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"}}}]
You could add the code you have so far?
– carlosfigueira