Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
Count number of all specific keys (or values) in json object/array- JavaScript/Angular?

I have large nested json object like this, and I want to count the number of pets in the whole object. How can achieve that? I've tired Object.keys(obj[0]).length but not successful of the desired result. Also how can I go deeper into array for counting for example some nested value like color in pet?

What is the good tutorial for working with multi level arrays in JavaScript or Angular?

obj = [
 {
        "person": {
          "name": "a",
        },
        "pet": {
          "name": "1"
        }
      },

      {
        "person": {
          "name": "b",
        },
        "pet": {
          "name": "2",
          "color": "black",
          }
        },

      {
        "person": {
          "name": "c",
        },
        "pet": {
          "name": "3",
          "color": "red",
          }
        }
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Filter the array with pet property and pet's color property and perform array count.

let obj = [
    {
        "person": {
            "name": "a",
        },
        "pet": {
            "name": "1"
        }
    },

    {
        "person": {
            "name": "b",
        },
        "pet": {
            "name": "2",
            "color": "black",
        }
    },

    {
        "person": {
            "name": "c",
        },
        "pet": {
            "name": "3",
            "color": "red",
        }
    }
];
let petItemCount = obj.filter(x => x["pet"]).length;
console.log(petItemCount);

let petItemWithColorCount = obj.filter(x => x["pet"] && x["pet"]["color"]).length;
console.log(petItemWithColorCount);

In case the pet value is possibly null or pet's color is possibly null,

let petItemCount = obj.filter(x => x["pet"] != undefined).length;
console.log(petItemCount);

let petItemWithColorCount = obj.filter(x => x["pet"] && x["pet"]["color"] != undefined).length;
console.log(petItemWithColorCount);
about 4 years ago · Juan Pablo Isaza Relatório

0

let pets = 0;
obj.map( item => {
  if(item.pet) {
    pets += 1;
  }
})
about 4 years ago · Juan Pablo Isaza Relatório

0

Sounds like reducing would be a fit:

colors = obj.reduce(
 (sum, { pet }) => pet.color !== undefined ? ++sum : sum, 
0)); // -> initial count value
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda