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

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

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

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

about 4 years ago · Juan Pablo Isaza Denunciar

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);
  }
});

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