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

132
Vistas
How to display all objects that contain all item from array - javascript

Need for some help here. Looking for a hint to solve this issue :

The goal is to filter arrayOfObject and get all objects with the property fruits containing all the element from the given array.

  const arrayOfObject = [
  {
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 2,
    country: 'Canada',
    fruits: ["APPLE"]
  },
  {
    id: 3,
    country: 'France',
    fruits: ["ORANGE", "BANANA", "LEMON"]
  },
  {
    id: 4,
    country: 'Mexico',
    fruits: ["BANANA", "PYTHON", "CHERRY"]
  },
  {
    id: 5,
    country: 'Ukraine',
    fruits: ["APPLE", "ORANGE", "CHERRY"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
];

First exemple with this given array :

const firstArrayOfFruits = ["APPLE","ORANGE","BANANA"];

Should render =>

[
  {
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
]

Second exemple with this given array :

const secondArrayOfFruits = ["APPLE","ORANGE"];

Should render =>

[
  {
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 5,
    country: 'Ukraine',
    fruits: ["APPLE", "ORANGE", "CHERRY"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use Array.prototype.filter:

const arrayOfObject = [{
    id: 1,
    country: 'USA',
    fruits: ["APPLE", "ORANGE", "BANANA"]
  },
  {
    id: 2,
    country: 'Canada',
    fruits: ["APPLE"]
  },
  {
    id: 3,
    country: 'France',
    fruits: ["ORANGE", "BANANA", "LEMON"]
  },
  {
    id: 4,
    country: 'Mexico',
    fruits: ["BANANA", "PYTHON", "CHERRY"]
  },
  {
    id: 5,
    country: 'Ukraine',
    fruits: ["APPLE", "ORANGE", "CHERRY"]
  },
  {
    id: 6,
    country: 'Italy',
    fruits: ["APPLE", "ORANGE", "BANANA", "LEMON", "CHERRY"]
  }
];

const firstArrayOfFruits = ["APPLE", "ORANGE", "BANANA"];

var arr = arrayOfObject.filter(item => item.fruits.filter(fruit => firstArrayOfFruits.indexOf(fruit) + 1).length >= firstArrayOfFruits.length);
console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using ES6 some() function is useful because when finds an entry it will return true and doesnt check the rest, so you can save some calcs, time and CPU

const filtered = arrayOfObject.filter(obj => {
       if(obj.fruits.some(fruit => fruitsLisToCheck.includes(fruit)))
         return obj
    })

const fruitsLisToCheck = ['BANANA', 'APPLE'];

const arrayOfObject = [
  {
    id: 2,
    country: 'Canada',
    fruits: ["APPLE"]
  },
  {
    id: 3,
    country: 'France',
    fruits: ["BANANA"]
  },
  {
    id: 4,
    country: 'Mexico',
    fruits: ["CHERRY"]
  },
];
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