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

152
Vistas
Javascript how to get length of an object inside an object

I have an object inside an object which and I want to get its length. I tried with Object.keys(obj).length but it shows the length of the parent object I want also to get the length of the child object.

Here's the code I tried.

let data = {
'data': {
  '0x': {
    0: { 
      0: 'test1', 
      330: 'test2'
    }
  }
 }
};

 let hideCtrls = false;
 if(Object.keys(data.data).length == 1){
    let objIn = Object.values(data.data);
    if(Object.values(objIn)[0] == 1) {
      hideCtrls = true;
     } else {
       hideCtrls = false;
     }
  };

console.log(hideCtrls);

I want to check the length of data inside data, if it's length is 1 I want to check the length of the values inside the first object (in this case it is 0x). If the length the child object is also 1 I want to set hideCtrls to true.

I want to get the length of the this last object 0: { 0: 'test1', 330: 'test2' }. Here we have two key value pairs inside so it should return false, if it has only one key value pair it should return true.

NOTE: The keys are dynamic

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Object.values returns an array. If you want the child object, you want to immediately index into it. In general, you can use these two helper functions. You just have to nest enough times to get to the object you care about:

let hasOneChild = (x) => Object.keys(x).length === 1;
let getOnlyChild = (x) => Object.values(x)[0];

let hideCtrls = false;
if(hasOneChild(data.data)){
    let x = getOnlyChild(data.data)
    if(hasOneChild(x)) {
        let y = getOnlyChild(x);
        ...
    } else {
        ...
    }
};
about 4 years ago · Santiago Trujillo Denunciar

0

let data = {
 data: { '0x': { 0: { 0: 'test1', 330: 'test2' } } }
}

function hideCtrls(){
 if(Object.keys(data['data'][Object.keys(data.data)[0]]['0']).length === 1) return true
 else return false
}

console.log(hideCtrls())

about 4 years ago · Santiago Trujillo Denunciar

0

Destructuring at least allows you to avoid the nightmare of a chained, computed accessor string. Here with a helper function for objectLength.

const objectLength = (obj) => Object.keys(obj).length;

const data = { data: { '0x': { 0: { 0: 'test1', 330: 'test2' } } } }

const parent = data.data;
const [{ 0: child }] = Object.values(parent);

const hideCtrls = (objectLength(parent) === 1 && objectLength(child) === 1);

console.log(hideCtrls)

about 4 years ago · Santiago Trujillo 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