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

108
Visualizações
Count the number of in-stock product variants from the dataLayer

I am working in Google Tag Manager on an ecomm site.

On the product pages I need to know the number of products that are in stock.

The dataLayer has sizes nested under each color option. Each size has a value showing if the size is in stock ("available": "false" or "true").

The code I am providing is what shows in the dataLayer. In this example, 3 of the 4 sizes are in stock, so I looking for the JS to run that will count the number of ""available": "true" - in this case the answer would be 3.

I am hoping for a solution that can be used in a variable in Google Tag Manager.

{
"product": {
    "variants": [
        {
            "colorID": "03",
            "colorName": "MEDIUM DENIM",
            "sizes": [
                {
                    "sizeID": "10",
                    "sizeName": "33",
                    "available": "false",
                    "price": "24.0"
                },
                {
                    "sizeID": "1",
                    "sizeName": "24",
                    "available": "true",
                    "price": "24.0"
                }
            ]
        },
        {
            "colorID": "06",
            "colorName": "LIGHT DENIM",
            "sizes": [
                {
                    "sizeID": "1",
                    "sizeName": "24",
                    "available": "true",
                    "price": "24.0"
                },
                {
                    "sizeID": "2",
                    "sizeName": "25",
                    "available": "true",
                    "price": "24.0"
                }
            ]
        }
    ]
}

}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If I understand correctly what result is expected

const data = {"product": {"variants": [
  {"colorID": "03","colorName": "MEDIUM DENIM","sizes": [
    {"sizeID": "10","sizeName": "33","available": "false","price": "24.0"},
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"}]},
  {"colorID": "06","colorName": "LIGHT DENIM","sizes": [
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"},
    {"sizeID": "2","sizeName": "25","available": "true","price": "24.0"}]}]}};
    

const result = {
  product: {
    variants: data.product.variants.map((variant) => (
      { 
        ...variant,  
        sizes: variant.sizes.filter(({ available }) => available === "true"),
      }
    ))
  }
}



console.dir(result, {depth: null})
.as-console-wrapper{min-height: 100%!important; top: 0}

---- update ---

const data = {"product": {"variants": [
  {"colorID": "03","colorName": "MEDIUM DENIM","sizes": [
    {"sizeID": "10","sizeName": "33","available": "false","price": "24.0"},
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"}]},
  {"colorID": "06","colorName": "LIGHT DENIM","sizes": [
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"},
    {"sizeID": "2","sizeName": "25","available": "true","price": "24.0"}]}]}};
    
const result = data.product.variants
  .flatMap((variant) => variant.sizes
    .filter(({ available }) => available === "true")
  ).length;

console.dir(result, {depth: null})
.as-console-wrapper{min-height: 100%!important; top: 0}

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