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

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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