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

344
Vistas
JSON - return number of items within a list that have a specific key value

Having a bit of a nightmare trying to solve this JSON problem. I need to store the number of offers as a tally, but it needs to only be a number whereby the offers have an activated status. In the example below (simplified for illustration) the result should be 2 (as only only of them is false for the 'activated' key). I've tried to target it with this:

var count = Object.keys(object.allOffers[0].offers[0].activated.hasOwnProperty('true')).length;

^^^ but it returns 0. Furthermore it only selects the first offer and I need a method that will target all of the offers that sit inside allOffers. Any ideas? Sample code below.

object {
  "allOffers": [
    {
      "offers": [
        {
          "offerId": 15661,
          "activated": true,
          "viewed": false
        },
        {
          "offerId": 15641,
          "activated": false,
          "viewed": false
        },
        {
          "offerId": 16461,
          "activated": true,
          "viewed": true
        }
      ]
    }
  ]
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your .offers[0] only targets the first element in the array, and checking whether the .activated string has an own property of 'true' doesn't make any sense.

Best method would be to use .reduce instead - iterate over the array, and add one to the accumulator for each object for which obj.activated is true.

const input = {
  "allOffers": [
    {
      "offers": [
        {
          "offerId": 15661,
          "activated": true,
          "viewed": false
        },
        {
          "offerId": 15641,
          "activated": false,
          "viewed": false
        },
        {
          "offerId": 16461,
          "activated": true,
          "viewed": true
        }
      ]
    }
  ]
};

const totalActivated = input.allOffers[0].offers
  .reduce((a, obj) => a + obj.activated, 0);
console.log(totalActivated);

Filtering by those that are activated and then checking the length of the resulting array works too.

const input = {
  "allOffers": [
    {
      "offers": [
        {
          "offerId": 15661,
          "activated": true,
          "viewed": false
        },
        {
          "offerId": 15641,
          "activated": false,
          "viewed": false
        },
        {
          "offerId": 16461,
          "activated": true,
          "viewed": true
        }
      ]
    }
  ]
};

const totalActivated = input.allOffers[0].offers
  .filter(obj => obj.activated)
  .length;
console.log(totalActivated);

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