Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
Comparación de dos matrices: eliminación de elementos de la matriz en función de las claves de objeto

Tengo dos objetos.

 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, }, ];

Como puede ver, ambos tienen la clave 'id'. Si los ID en arrayOne no están en arrayTwo, me gustaría que se eliminen de arrayOne (el objeto completo). Entonces, en este caso, solo el objeto con "tamaños" debe eliminarse de arrayOne. ¿Cómo haría para hacer esto? ¡Gracias por adelantado!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

podrías utilizar el filter :

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

0

Podría usar un Set con id y filtrar la otra matriz.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!