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

117
Visualizações
Javascript json filter with arrow function

I was wondering how I can filter the values "tags" in the json above using an arrow function:

const ttag = [{
    "code": 795302828,
    "code_integration": "123",
    "company": "ACME LTD",
    "phone": "135575788",
    "tags": [{"tag": "companyAA"},
             {"tag": "companyBB"},
             {"tag": "companyCC"},
             {"tag": "companyDD"}
            ],
    "status": "Y"
}]

const onlyTags = ttag.filter(f => f.tags)
console.log(onlyTags)

expected result: ['companyAA', 'companyBB', 'companyCC', 'companyDD']

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

0

First of all, that's not how filter method works. Filter returns an array with values that are true for function passed as the argument. More you can read here.

You could use map & flatMap, like this:

ttag.flatMap(f => f.tags.map(t => t.tag));

map returns array of values specified in the passed method. flatMap does the same, but if the result is an array it flattens it, so result is an array of strings instead of an array of arrays of strings.

Important

flatMap is not supported in Internet Explorer, but it's already unsupported browser so I wouldn't bother.

about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe an ugly way to do it, but with the first .map you get each item of the main array, and with the second .map an array of tags inside it.

const ttag = [{
    "code": 795302828,
    "code_integration": "123",
    "company": "ACME LTD",
    "phone": "135575788",
    "tags": [{"tag": "companyAA"},
             {"tag": "companyBB"},
             {"tag": "companyCC"},
             {"tag": "companyDD"}
            ],
    "status": "Y"
}]

const onlyTags = ttag.map(f => f.tags.map(t => t.tag));
console.log(onlyTags);
console.log(onlyTags[0]); // expected output

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's one solution using map and reduce. At the end the array is flatten so it returns only an array containing the city names

const ttag = [{
  "code": 795302828,
  "code_integration": "123",
  "company": "ACME LTD",
  "phone": "135575788",
  "tags": [{
      "tag": "companyAA"
    },
    {
      "tag": "companyBB"
    },
    {
      "tag": "companyCC"
    },
    {
      "tag": "companyDD"
    }
  ],
  "status": "Y"
}]

const result = ttag.map(obj => obj.tags.reduce((acc, next) => [...acc, next.tag], [])).flat()
console.log(result)

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