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

274
Vistas
Comparing two arrays - removing element from array based on object keys

I have two objects.

const arrayOne = [
  {
label: "Categories",
to: "/categories",
id: "product_type",
  },
  {
label: "Colors",
to: "/colors",
id: "color",
  },
  {
label: "Materials",
to: "/materials",
id: "material",
  },
  {
label: "Sizes",
to: "/sizes",
id: "sizes",
  },
  {
label: "Designers",
to: "/designers",
id: "designer_slug",
  },
  {
label: "Stores",
to: "/stores",
id: "retailer_slug",
  },
];

const arrayTwo = [
{
  id: "gender",
  label: "Gender",
  lazy_loaded: false,
},
{
  id: "product_type",
  label: "Category",
  lazy_loaded: false,
},
{
  id: "quick_filters",
  label: "Quick filters",
  lazy_loaded: false,
},
{
  id: "final_price",
  label: "Price",
  lazy_loaded: false,
},
{
  id: "color",
  label: "Color",
  lazy_loaded: false,
},
{
  id: "material",
  label: "Material",
  lazy_loaded: false,
},
{
  id: "designer_slug",
  label: "Brand",
  lazy_loaded: true,
},
{
  id: "retailer_slug",
  label: "Store",
  lazy_loaded: true,
},
  ];

As you can see they both have the key 'id'. If the IDs in arrayOne aren't in arrayTwo, I would like them to be removed from arrayOne (the whole object). So in this case, only the object with "sizes" should be removed from arrayOne. How would I go about doing this? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you could utilize filter:

const newArrayOne = arrayOne.filter(x => arrayTwo.find(y => y.id === x.id))
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use a Set with id and filter the other array.

const
    arrayOne = [{ label: "Categories", to: "/categories", id: "product_type" }, { label: "Colors", to: "/colors", id: "color" }, { label: "Materials", to: "/materials", id: "material" }, { label: "Sizes", to: "/sizes", id: "sizes" }, { label: "Designers", to: "/designers", id: "designer_slug" }, { label: "Stores", to: "/stores", id: "retailer_slug" }],
    arrayTwo = [{ id: "gender", label: "Gender", lazy_loaded: false }, { id: "product_type", label: "Category", lazy_loaded: false }, { id: "quick_filters", label: "Quick filters", lazy_loaded: false }, { id: "final_price", label: "Price", lazy_loaded: false }, { id: "color", label: "Color", lazy_loaded: false }, { id: "material", label: "Material", lazy_loaded: false }, { id: "designer_slug", label: "Brand", lazy_loaded: true }, { id: "retailer_slug", label: "Store", lazy_loaded: true }],
    two = new Set(arrayTwo.map(({ id }) => id)),
    result = arrayOne.filter(({ id }) => two.has(id));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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