Home > Mobile >  replace the array of elements with the help of another array of elements if its have both common val
replace the array of elements with the help of another array of elements if its have both common val

Time:04-14

Presented below is the question

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 its matched take name value and set thet 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 what 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. Please could anyone help out

CodePudding user response:

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

  • Related