Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
Remove a string using Ternary operation in JavaScript

I have something like this in my console: How can I take out the [object Object] from my output?

I want the output printed without the [Object Object]. Probably using a ternary operation?

data:[object Object]
  text:Data is supplied by Government
  link:https://en.wikipedia.org/

This is my code:

const GetData = (obj, step) => {

    let padSpace = ""

    for (let j=0; j < step; j++) {
      padSpace += ''
    }

    for (let k in obj) {
      console.log(padSpace + k + ':' + obj[k])
      if (typeof(obj[k]) === "object") {
        if (Array.isArray(obj[k])) {
          obj[k].forEach(element => {
              if (typeof(element === "object")) {
                GetData(element, step + 1);
              } else {
                console.log(padSpace + element)
              }
            }
          );
        } else {
          GetData(obj[k], step + 1);
        }
      } else {}
    }

I want something like:

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I am not an expert but I'll try to help here.

This data is coming from an external API, I suppose. Right? It seems to me that is just a matter of how the data is being sent back from the server. Maybe you just need to use JSON.stringfy to convert the object to string.

It would be nice to have access to the kind of data being sent from the server to manipulate it.

Hope it starts to help you in a way...

Cheers

about 4 years ago · Juan Pablo Isaza Denunciar

0

The following test will always pass because typeof true and typeof false are both "boolean" which is truthy.

if (typeof(element === "object")) {

Try

if (typeof element === "object") {

I have no idea why you are talking about a ternary operation.


To avoid the [object Object] you can move the existing console.log and add another one as shown:

for (let prop in entity) {  
  if (typeof entity[prop] === "object") {
    console.log(padString + prop + ':');
    if (Array.isArray(entity[prop])) {
      entity[prop].forEach(element => {
        if (typeof element === "object") {
          formatData(element, level + 1);
        } else {
          console.log(padString + element)
        }
      });
    } else {
      formatData(entity[prop], level + 1);
    }
  } else {
    console.log(padString + prop + ':' + entity[prop])
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda