Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you could utilize filter:

const newArrayOne = arrayOne.filter(x => arrayTwo.find(y => y.id === x.id))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading