• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

199
Views
Remove mirrored objects from an array

I have an issue which I dont't really know how to tackle in a good way

I have an array of objects which looks roughly like this:

[
    { name: "horse", newName: "owl" }
    { name: "owl", newName: "horse" }
    { name: "frog", newName: "dog" }
]

I want to remove "mirrored" objects from this array, in result having an array like this:

[
    { name: "frog", newName: "dog" }
]

Basically I need to find objects with opposite keys and values

More complex scenarios:

[
    { name: "horse", newName: "frog" }
    { name: "owl", newName: "horse" }
    { name: "frog", newName: "owl" }
]

    // Result: []
[
    { name: "horse", newName: "frog" }
    { name: "dog", newName: "cat" }
    { name: "owl", newName: "horse" }
    { name: "frog", newName: "owl" }
    { name: "monkey", newName: "worm" }
    { name: "cat", newName: "dog" }
]

    // Result: [{ name: "monkey", newName: "worm" }]

In the first case I would simply loop through the array and if an object like this is found:

{name: obj1.value2, newName: obj1.value1} I would splice them both

But I have no idea how to approach the more complex situation when 3 or more objects would have to be removed. Any hints?

In advance thanks for your time

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can do it by using filter function on array

var data = [
    { name: "horse", newName: "frog" },
    { name: "dog", newName: "cat" },
    { name: "owl", newName: "horse" },
    { name: "frog", newName: "owl" },
    { name: "monkey", newName: "worm" },
    { name: "cat", newName: "dog" }
];


let noMirrored = data.filter(one => {
  return (
    !data.some(element => element.name === one.newName) &&
    !data.some(element => element.newName === one.name)
  );
});

console.log(noMirrored);

almost 3 years ago · Juan Pablo Isaza Report

0

Use some to check the present value.

var array = [{
    name: "horse",
    newName: "frog"
  },
  {
    name: "dog",
    newName: "cat"
  },
  {
    name: "owl",
    newName: "horse"
  },
  {
    name: "frog",
    newName: "owl"
  },
  {
    name: "monkey",
    newName: "worm"
  },
  {
    name: "cat",
    newName: "dog"
  }
]

array.forEach(t => {

  var nameFound = array.some(a => a.name == t.newName);
  var newNameFound = array.some(a => a.newName == t.name);
  if (!nameFound && !newNameFound) {
    console.log(t);
  }
});

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error