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

113
Vistas
Replace the array of elements using another array of elements if its have both common value

Below is the referenceArray to compare and manipulate originalArray

const referenceArray = [
                       { name: 'animal', source: ['duck', 'cat'], target: ['water', 'ground'] },
                       { name: 'car', source: ['tata', 'kia'], target: ['tiago', 'sector'] },
                       { name: 'bike', source: ['honda', 'hero'], target: ['livo', 'xtream'] },
                       { name: 'vehicle', source: ['honda', 'hero'], target: ['hard', 'soft'] },
                       ];

And this is I want to change the array originalArray source and target values with the help of referenceArray source and target if it's matched take name value and set that value to originalArray source and target.

const originalArray = [
    { source: 'water', target: 'hero' },
    { source: 'tata', target: 'ground' },
    { source: 'livo', target: 'kia' },
    { source: 'hero', target: 'sector' },
  ];

Expected output that is required:

 const output1 = [
    { source: 'animal', target: ['bike', 'vehicle'] },
    { source: 'car', target: 'animal' },
    { source: 'bike', target: 'car' },
    { source: ['vehicle', 'bike'], target: 'car' },
  ];


 const output2 = [
    { source: 'animal', target: 'bike' },
    { source: 'animal', target: 'vehicle' },
    { source: 'car', target: 'animal' },
    { source: 'bike', target: 'car' },
    { source: 'vehicle', target: 'car' },
    { source: 'bike', target: 'car' },
  ];

Either output is acceptable, however output2 is preferred.

I'm confused how to achieve this format without getting key value conflicts.

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

0

The below may be one possible solution to achieve the desired objective (Output-2, from the question above).

Code Snippet

// get output in format-2
const getOutput2 = (refArr, arr) => (
  arr.flatMap(        // iterate using ".flatMap" to avoid nested result
    ({ source : osrc, target : otgt }) => (
      refArr.filter(      // filter for "source" using ref-arr
        ob => ob?.source.includes(osrc) || ob?.target.includes(osrc)
      ).sort(             // sort descending to match order of output-2
        (a, b) => b.name > a.name ? 1 : -1
      ).flatMap(          // for each "source", iterate using ".flatMap"
        ob => refArr.filter(    // filter for "target" 
          ob2 => ob2?.source.includes(otgt) || ob2?.target.includes(otgt)
        ).sort(           // sort ascending to match order of output-2
          (a, b) => a.name > b.name ? 1 : -1
        ).map(ob2 => ({         // construct the desired objective array
          source: ob.name, target: ob2.name
        }))
      )
    )
  )
);

// reference array from question
const referenceArray = [{
    name: 'animal',
    source: ['duck', 'cat'],
    target: ['water', 'ground']
  },
  {
    name: 'car',
    source: ['tata', 'kia'],
    target: ['tiago', 'sector']
  },
  {
    name: 'bike',
    source: ['honda', 'hero'],
    target: ['livo', 'xtream']
  },
  {
    name: 'vehicle',
    source: ['honda', 'hero'],
    target: ['hard', 'soft']
  },
];

// original array from question
const originalArray = [{
    source: 'water',
    target: 'hero'
  },
  {
    source: 'tata',
    target: 'ground'
  },
  {
    source: 'livo',
    target: 'kia'
  },
  {
    source: 'hero',
    target: 'sector'
  },
];

// log the results to console for verification
console.log(getOutput2(referenceArray, originalArray));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Explanation

Comments added inline in the snippet above

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