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

392
Vistas
JavaScript, filter array of objects by a specific key's value

I am trying to find the best way to filter my array of objects with specific key's string. Basically what I am trying to achieve is to get the objects which contain "Type":"Blue". Here is my data:

[
   {
       "data": [
           {}
       ],
       "Name": "1",
       "Type": "Blue"
   },
   {
       "data": [
           {}
       ],
       "Name": "2",
       "Type": "Red"
   },
   {
       "data": [
           {}
       ],
       "Name": "3",
       "Type": "Blue"
    }
] 
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use the filter method. See the snippet below, as well as a definition of the method from MDN:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

const data = [
  {
    data: [{}],
    Name: "1",
    Type: "Blue"
  },
  {
    data: [{}],
    Name: "2",
    Type: "Red"
  },
  {
    data: [{}],
    Name: "3",
    Type: "Blue"
  }
];

const filteredData = data.filter((item) => item.Type === "Blue");
console.log(filteredData);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the Array.prototype.filter() method.

arr.filter(obj => obj.Type == 'Blue');

will give you the array containing only the objects with type Blue.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understood your requirement correctly, You have a string "Type: Blue" and based on this string you have to filtered out the data array dynamically based on the key as Type and value as Blue. If Yes, Here you go :

const str = "Type: Blue";

const splittedStr = str.split(':');

const data = [
  {
    Name: "1",
    Type: "Blue"
  },
  {
    Name: "2",
    Type: "Red"
  },
  {
    Name: "3",
    Type: "Blue"
  }
];

const filteredData = data.filter((item) => item[splittedStr[0]] === splittedStr[1].trim());

console.log(filteredData);

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