JSON eliminar claves con su valor (Vacío) en javascript

publicado por: Anonymous

Tengo un json a validar y quiero eliminar los valores vacios o con la palabra 'Vacío' para no tener datos innecesarios

var objeto = {clave1:"hola",clave2:"Vacío",clave3:""}

la idea seria eliminar la clave2 porque tiene el valor Vacío y la calve3 porque esta vacía ("") esto es fácil seria algo así

_x000D_

_x000D_

var objeto = {clave1:"hola",clave2:"Vacío",clave3:""}_x000D_
_x000D_
for (var clave in objeto) {_x000D_
    if(objeto[clave]==""||objeto[clave]=="Vacío"){_x000D_
     delete objeto[clave] //eliminamos solo las claves vacias_x000D_
    }_x000D_
  }_x000D_
  _x000D_
console.log(objeto)

_x000D_

_x000D_

_x000D_

El problema es cuando el json es muy complejo y tiene muchos objetos hijos y hay que validar todos y quitarles las claves vacias sin saber la forma o el orden del objeto

JSON a validar

_x000D_

_x000D_

var objeto = _x000D_
  {_x000D_
    "state": "",_x000D_
    "title": "Evaluador",_x000D_
    "tituloPadre": "",_x000D_
    "id": 26,_x000D_
    "data": {_x000D_
      "tipoLicencia": 26,_x000D_
      "categorias": 0,_x000D_
      "nroLicencia": {_x000D_
        "valor": "1234",_x000D_
        "activo": true_x000D_
      },_x000D_
      "fechaEmision": {_x000D_
        "valor": "16-12-2017",_x000D_
        "activo": true_x000D_
      },_x000D_
      "fechaVencimiento": {_x000D_
        "valor": "22-12-2017",_x000D_
        "activo": true_x000D_
      },_x000D_
      "estatus": {_x000D_
        "valor": "2",_x000D_
        "activo": true_x000D_
      },_x000D_
      "horasTotalesVuelo": {_x000D_
        "valor": "21",_x000D_
        "activo": true_x000D_
      },_x000D_
      "observacionesImpresasLicencia": {_x000D_
        "valor": "sdfg",_x000D_
        "activo": true_x000D_
      },_x000D_
      "soporteCargarLicencias": {_x000D_
        "valor": "Vacío",_x000D_
        "activo": true_x000D_
      },_x000D_
      "habilitacionesClase": {_x000D_
        "clasificacionHabilitacion": 1,_x000D_
        "activo": true,_x000D_
        "registros": [_x000D_
          {_x000D_
            "idTipoHabilitacion": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 0_x000D_
            },_x000D_
            "idFuncion": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 7_x000D_
            },_x000D_
            "manufactura": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 5_x000D_
            },_x000D_
            "modelo": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 4_x000D_
            },_x000D_
            "siglasTipoDesignador": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 3_x000D_
            },_x000D_
            "tipoAeronave": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 0_x000D_
            },_x000D_
            "cantidadMotores": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 2_x000D_
            },_x000D_
            "fechaVencimiento": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 6_x000D_
            },_x000D_
            "estatus": {_x000D_
              "clave": "",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 8_x000D_
            },_x000D_
            "tipoMotor": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": false,_x000D_
              "index": 1_x000D_
            },_x000D_
            "soporte": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": false,_x000D_
              "index": 0_x000D_
            }_x000D_
          }_x000D_
        ]_x000D_
      },_x000D_
      "habilitacionesTipo": {_x000D_
        "clasificacionHabilitacion": 2,_x000D_
        "activo": true,_x000D_
        "registros": [_x000D_
          {_x000D_
            "idTipoHabilitacion": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 0_x000D_
            },_x000D_
            "idFuncion": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 7_x000D_
            },_x000D_
            "manufactura": {_x000D_
              "clave": "",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 5_x000D_
            },_x000D_
            "modelo": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 4_x000D_
            },_x000D_
            "siglasTipoDesignador": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 3_x000D_
            },_x000D_
            "tipoAeronave": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 0_x000D_
            },_x000D_
            "cantidadMotores": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 2_x000D_
            },_x000D_
            "fechaVencimiento": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 6_x000D_
            },_x000D_
            "estatus": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 8_x000D_
            },_x000D_
            "tipoMotor": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 1_x000D_
            },_x000D_
            "soporte": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 9_x000D_
            }_x000D_
          }_x000D_
        ]_x000D_
      },_x000D_
      "habilitacionesEspeciales": {_x000D_
        "clasificacionhabilitacion": 3,_x000D_
        "activo": false,_x000D_
        "vueloInstrumental": {_x000D_
          "activo": true,_x000D_
          "idTipoHabilitacion": {_x000D_
            "clave": "",_x000D_
            "valor": 17,_x000D_
            "edit": "false"_x000D_
          },_x000D_
          "fechaVencimiento": {_x000D_
            "clave": "Vacío",_x000D_
            "valor": "",_x000D_
            "edit": "false"_x000D_
          },_x000D_
          "estatus": {_x000D_
            "clave": "Vacío",_x000D_
            "valor": "",_x000D_
            "edit": "false"_x000D_
          }_x000D_
        },_x000D_
        "fumigacionAerea": {_x000D_
          "activo": true,_x000D_
          "registros": [_x000D_
            {_x000D_
              "idTipoHabilitacion": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 0_x000D_
              },_x000D_
              "idFuncion": {_x000D_
                "clave": "",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 7_x000D_
              },_x000D_
              "manufactura": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 5_x000D_
              },_x000D_
              "modelo": {_x000D_
                "clave": "",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 4_x000D_
              },_x000D_
              "siglasTipoDesignador": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 3_x000D_
              },_x000D_
              "tipoAeronave": {_x000D_
                "clave": "",_x000D_
                "valor": "",_x000D_
                "edit": "false",_x000D_
                "index": 0_x000D_
              },_x000D_
              "cantidadMotores": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 2_x000D_
              },_x000D_
              "fechaVencimiento": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 6_x000D_
              },_x000D_
              "estatus": {_x000D_
                "clave": "",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 8_x000D_
              },_x000D_
              "tipoMotor": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "Vacío",_x000D_
                "edit": "false",_x000D_
                "index": 1_x000D_
              },_x000D_
              "soporte": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "",_x000D_
                "edit": "false",_x000D_
                "index": 9_x000D_
              }_x000D_
            }_x000D_
          ]_x000D_
        }_x000D_
      },_x000D_
      "competenciaLinguistica": {_x000D_
        "clasificacionhabilitacion": 4,_x000D_
        "activo": false,_x000D_
        "registros": [_x000D_
          {_x000D_
            "nivel": {_x000D_
              "clave": "",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 0_x000D_
            },_x000D_
            "duracion": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 1_x000D_
            },_x000D_
            "fechaVencimiento": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 2_x000D_
            },_x000D_
            "estatus": {_x000D_
              "clave": "",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 3_x000D_
            }_x000D_
          }_x000D_
        ]_x000D_
      },_x000D_
      "habilitaciones": {_x000D_
        "clasificacionHabilitacion": 5,_x000D_
        "activo": false,_x000D_
        "idTipoHabilitacion": {_x000D_
          "clave": "Vacío",_x000D_
          "valor": "",_x000D_
          "edit": "false"_x000D_
        },_x000D_
        "fechaVencimiento": {_x000D_
          "clave": "",_x000D_
          "valor": "",_x000D_
          "edit": "false"_x000D_
        },_x000D_
        "estatus": {_x000D_
          "clave": "Vacío",_x000D_
          "valor": "Vacío",_x000D_
          "edit": "false"_x000D_
        }_x000D_
      },_x000D_
      "habilitacionesRpa": {_x000D_
        "clasificacionHabilitacion": 6,_x000D_
        "activo": false,_x000D_
        "registros": [_x000D_
          {_x000D_
            "idTipoHabilitacion": {_x000D_
              "clave": "",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 0_x000D_
            },_x000D_
            "fechaVencimiento": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 1_x000D_
            },_x000D_
            "estatus": {_x000D_
              "clave": "",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 2_x000D_
            }_x000D_
          }_x000D_
        ]_x000D_
      },_x000D_
      "habilitacionesTripulanteVuelo": {_x000D_
        "clasificacionHabilitacion": 5,_x000D_
        "activo": false,_x000D_
        "registros": [_x000D_
          {_x000D_
            "idTipoHabilitacion": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": "false",_x000D_
              "index": 0_x000D_
            },_x000D_
            "fechaVencimiento": {_x000D_
              "clave": "",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 1_x000D_
            },_x000D_
            "estatus": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false",_x000D_
              "index": 2_x000D_
            }_x000D_
          }_x000D_
        ]_x000D_
      }_x000D_
    }_x000D_
  }_x000D_
  _x000D_
_x000D_
  console.log(objeto)

_x000D_

_x000D_

_x000D_

solución

Para iterar un JSON dinamicamente sin saber su tamaño o forma podemos usar una funcion re cursiva

Terminos

Recursividad

Como definición general, podemos decir que una función recursiva es aquella que se llama a si misma para resolverse. Dicho de otra manera, una función recursiva se resuelve con una llamada a si misma, cambiando el valor de un parámetro en la llamada a la función. A través de las sucesivas llamadas recursivas a la función se van obteniendo valores que, computados, sirven para obtener el valor de la función llamada originalmente.


typeof

El operador typeof devuelve una cadena que indica el tipo del operando sin evaluarlo. operando es la cadena, variable, palabra clave u objeto para el que se devolverá su tipo. Los paréntesis son opcionales.


Solucion

para resolver este problema creamos una funcion re cursiva que se llama a si misma cuando se cumpla una condición,en este caso hay que preguntar por el tipo de elemento si es un objeto entonces volvemos a ejecutar la misma funcion con el parametro del objeto que en ese momento esta siendo iterado y así hasta recorrerlo todo.

function eliminarVacios(jsonx){ <--- funcion recursiva
 for (var clave in jsonx) { <--- for que itera el json pasado como parametro
  if(typeof jsonx[clave] == 'string'){ <-- preguntamos si es un 'string'
    if(jsonx[clave] == 'Vacío'||jsonx[clave] == ''){ <-- validamos si esta vacio o contiene la palabra 'Vacío'
      delete jsonx[clave] <-- eliminamos la clave que este vacia o contenga la palabra 'Vacío'
    }
  } else if (typeof jsonx[clave] == 'object') { <-- preguntamos si es un 'objeto'
    eliminarVacios(jsonx[clave]) <-- volvemos a ejecutar la funcion recursiva
  }
 }
}

Ejemplo funcional

_x000D_

_x000D_

var objeto = _x000D_
      {_x000D_
        "state": "",_x000D_
        "title": "Evaluador",_x000D_
        "tituloPadre": "",_x000D_
        "id": 26,_x000D_
        "data": {_x000D_
          "tipoLicencia": 26,_x000D_
          "categorias": 0,_x000D_
          "nroLicencia": {_x000D_
            "valor": "1234",_x000D_
            "activo": true_x000D_
          },_x000D_
          "fechaEmision": {_x000D_
            "valor": "16-12-2017",_x000D_
            "activo": true_x000D_
          },_x000D_
          "fechaVencimiento": {_x000D_
            "valor": "22-12-2017",_x000D_
            "activo": true_x000D_
          },_x000D_
          "estatus": {_x000D_
            "valor": "2",_x000D_
            "activo": true_x000D_
          },_x000D_
          "horasTotalesVuelo": {_x000D_
            "valor": "21",_x000D_
            "activo": true_x000D_
          },_x000D_
          "observacionesImpresasLicencia": {_x000D_
            "valor": "sdfg",_x000D_
            "activo": true_x000D_
          },_x000D_
          "soporteCargarLicencias": {_x000D_
            "valor": "Vacío",_x000D_
            "activo": true_x000D_
          },_x000D_
          "habilitacionesClase": {_x000D_
            "clasificacionHabilitacion": 1,_x000D_
            "activo": true,_x000D_
            "registros": [_x000D_
              {_x000D_
                "idTipoHabilitacion": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 0_x000D_
                },_x000D_
                "idFuncion": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 7_x000D_
                },_x000D_
                "manufactura": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 5_x000D_
                },_x000D_
                "modelo": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 4_x000D_
                },_x000D_
                "siglasTipoDesignador": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 3_x000D_
                },_x000D_
                "tipoAeronave": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 0_x000D_
                },_x000D_
                "cantidadMotores": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 2_x000D_
                },_x000D_
                "fechaVencimiento": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 6_x000D_
                },_x000D_
                "estatus": {_x000D_
                  "clave": "",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 8_x000D_
                },_x000D_
                "tipoMotor": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "",_x000D_
                  "edit": false,_x000D_
                  "index": 1_x000D_
                },_x000D_
                "soporte": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": false,_x000D_
                  "index": 0_x000D_
                }_x000D_
              }_x000D_
            ]_x000D_
          },_x000D_
          "habilitacionesTipo": {_x000D_
            "clasificacionHabilitacion": 2,_x000D_
            "activo": true,_x000D_
            "registros": [_x000D_
              {_x000D_
                "idTipoHabilitacion": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 0_x000D_
                },_x000D_
                "idFuncion": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 7_x000D_
                },_x000D_
                "manufactura": {_x000D_
                  "clave": "",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 5_x000D_
                },_x000D_
                "modelo": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 4_x000D_
                },_x000D_
                "siglasTipoDesignador": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 3_x000D_
                },_x000D_
                "tipoAeronave": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 0_x000D_
                },_x000D_
                "cantidadMotores": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 2_x000D_
                },_x000D_
                "fechaVencimiento": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 6_x000D_
                },_x000D_
                "estatus": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 8_x000D_
                },_x000D_
                "tipoMotor": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 1_x000D_
                },_x000D_
                "soporte": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 9_x000D_
                }_x000D_
              }_x000D_
            ]_x000D_
          },_x000D_
          "habilitacionesEspeciales": {_x000D_
            "clasificacionhabilitacion": 3,_x000D_
            "activo": false,_x000D_
            "vueloInstrumental": {_x000D_
              "activo": true,_x000D_
              "idTipoHabilitacion": {_x000D_
                "clave": "",_x000D_
                "valor": 17,_x000D_
                "edit": "false"_x000D_
              },_x000D_
              "fechaVencimiento": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "",_x000D_
                "edit": "false"_x000D_
              },_x000D_
              "estatus": {_x000D_
                "clave": "Vacío",_x000D_
                "valor": "",_x000D_
                "edit": "false"_x000D_
              }_x000D_
            },_x000D_
            "fumigacionAerea": {_x000D_
              "activo": true,_x000D_
              "registros": [_x000D_
                {_x000D_
                  "idTipoHabilitacion": {_x000D_
                    "clave": "Vacío",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 0_x000D_
                  },_x000D_
                  "idFuncion": {_x000D_
                    "clave": "",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 7_x000D_
                  },_x000D_
                  "manufactura": {_x000D_
                    "clave": "Vacío",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 5_x000D_
                  },_x000D_
                  "modelo": {_x000D_
                    "clave": "",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 4_x000D_
                  },_x000D_
                  "siglasTipoDesignador": {_x000D_
                    "clave": "Vacío",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 3_x000D_
                  },_x000D_
                  "tipoAeronave": {_x000D_
                    "clave": "",_x000D_
                    "valor": "",_x000D_
                    "edit": "false",_x000D_
                    "index": 0_x000D_
                  },_x000D_
                  "cantidadMotores": {_x000D_
                    "clave": "Vacío",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 2_x000D_
                  },_x000D_
                  "fechaVencimiento": {_x000D_
                    "clave": "Vacío",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 6_x000D_
                  },_x000D_
                  "estatus": {_x000D_
                    "clave": "",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 8_x000D_
                  },_x000D_
                  "tipoMotor": {_x000D_
                    "clave": "Vacío",_x000D_
                    "valor": "Vacío",_x000D_
                    "edit": "false",_x000D_
                    "index": 1_x000D_
                  },_x000D_
                  "soporte": {_x000D_
                    "clave": "Vacío",_x000D_
                    "valor": "",_x000D_
                    "edit": "false",_x000D_
                    "index": 9_x000D_
                  }_x000D_
                }_x000D_
              ]_x000D_
            }_x000D_
          },_x000D_
          "competenciaLinguistica": {_x000D_
            "clasificacionhabilitacion": 4,_x000D_
            "activo": false,_x000D_
            "registros": [_x000D_
              {_x000D_
                "nivel": {_x000D_
                  "clave": "",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 0_x000D_
                },_x000D_
                "duracion": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 1_x000D_
                },_x000D_
                "fechaVencimiento": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 2_x000D_
                },_x000D_
                "estatus": {_x000D_
                  "clave": "",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 3_x000D_
                }_x000D_
              }_x000D_
            ]_x000D_
          },_x000D_
          "habilitaciones": {_x000D_
            "clasificacionHabilitacion": 5,_x000D_
            "activo": false,_x000D_
            "idTipoHabilitacion": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "",_x000D_
              "edit": "false"_x000D_
            },_x000D_
            "fechaVencimiento": {_x000D_
              "clave": "",_x000D_
              "valor": "",_x000D_
              "edit": "false"_x000D_
            },_x000D_
            "estatus": {_x000D_
              "clave": "Vacío",_x000D_
              "valor": "Vacío",_x000D_
              "edit": "false"_x000D_
            }_x000D_
          },_x000D_
          "habilitacionesRpa": {_x000D_
            "clasificacionHabilitacion": 6,_x000D_
            "activo": false,_x000D_
            "registros": [_x000D_
              {_x000D_
                "idTipoHabilitacion": {_x000D_
                  "clave": "",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 0_x000D_
                },_x000D_
                "fechaVencimiento": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 1_x000D_
                },_x000D_
                "estatus": {_x000D_
                  "clave": "",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 2_x000D_
                }_x000D_
              }_x000D_
            ]_x000D_
          },_x000D_
          "habilitacionesTripulanteVuelo": {_x000D_
            "clasificacionHabilitacion": 5,_x000D_
            "activo": false,_x000D_
            "registros": [_x000D_
              {_x000D_
                "idTipoHabilitacion": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "",_x000D_
                  "edit": "false",_x000D_
                  "index": 0_x000D_
                },_x000D_
                "fechaVencimiento": {_x000D_
                  "clave": "",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 1_x000D_
                },_x000D_
                "estatus": {_x000D_
                  "clave": "Vacío",_x000D_
                  "valor": "Vacío",_x000D_
                  "edit": "false",_x000D_
                  "index": 2_x000D_
                }_x000D_
              }_x000D_
            ]_x000D_
          }_x000D_
        }_x000D_
      }_x000D_
_x000D_
_x000D_
  function eliminarVacios(jsonx){_x000D_
    for (var clave in jsonx) {_x000D_
      if(typeof jsonx[clave] == 'string'){_x000D_
        if(jsonx[clave] == 'Vacío'||jsonx[clave] == ''){_x000D_
          delete jsonx[clave]_x000D_
        }_x000D_
      } else if (typeof jsonx[clave] == 'object') {_x000D_
        eliminarVacios(jsonx[clave])_x000D_
      }_x000D_
    }_x000D_
  }_x000D_
  _x000D_
  _x000D_
  eliminarVacios(objeto)_x000D_
  console.log(objeto)

_x000D_

_x000D_

_x000D_

Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *