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

273
Visualizações
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 Respostas
Responde à pergunta

0

you could utilize filter:

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

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