Home > Software engineering >  find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in A
find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in A

Time:02-16

I have two arrays. look by id, find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in Array2, and make a new Array where element in Array1 is moved to the new index found in Array2. in Array1 {id: 001} is at index 0, I would like it to be in a new Array at index 1 (index it is FIRST found in Array2). {id: 002} is at index 1 in Array1, I would like this to be in the new Array at index 3( index it is FIRST found in Array2) so on....

const Array1 = [
    {
      id: '001',
      school: "blue springs"
    },
    {
      id: '002',
      school: "sycamore hills"
    },
    {
      id: '003',
      school: "moreland ridge"
    },
    {
      id: '004',
      school: "grain valley"
    }
]


const Array2 = [
         {
      id: '003',
      participant: "Susan"
    },
    {
      id: '001',
      participant: "Henry"
    },
    {
      id: '003',
      participant: "Justin" <---- if 003 exists do not duplicate the return array, this is my issue....
    },
    {
      id: '004',
      participant: "Jessica"
    },
    {
      id: '002',
      participant: "Carly"
    },
    {
      id: '001',
      participant: "Chloe"  <---- if 001 exists do not duplicate the return array, this is my issue....
    }

// got this far, 

const finder = Array1.map((el) => { return Array2.findIndex((el2) => { return el2.id === el.id}) })


console.log(finder)
// [ 1, 4, 0, 3 ] <--- need to move Array1 objects to these new indexes 

expected output

const Result = [
    {
      id: '003',
      school: "moreland ridge"
    },
    {
      id: '001',
      school: "blue springs"
    },
    {
      id: '004',
      school: "grain valley"
    },
    {
      id: '002',
      school: "sycamore hills"
    }
]

CodePudding user response:

You first filter to remove duplicates on Array2, and then look for a match in Array1 regarding id's

const Array1 = [
    {
      id: '001',
      school: "blue springs"
    },
    {
      id: '002',
      school: "sycamore hills"
    },
    {
      id: '003',
      school: "moreland ridge"
    },
    {
      id: '004',
      school: "grain valley"
    }
]


const Array2 = [
         {
      id: '003',
      participant: "Susan"
    },
    {
      id: '001',
      participant: "Henry"
    },
    {
      id: '003',
      participant: "Justin" // <---- if 003 exists do not duplicate the return array, this is my issue....
    },
    {
      id: '004',
      participant: "Jessica"
    },
    {
      id: '002',
      participant: "Carly"
    },
    {
      id: '001',
      participant: "Chloe" // <---- if 001 exists do not duplicate the return array, this is my issue....
    }
]
const alreadyShown = {};
const res = Array2.filter( el => {
  if (!alreadyShown[el.id]){
    alreadyShown[el.id] = true;
    return true;
  }
  return false;
}).map( x => Array1.find( y => x.id === y.id ) || x);


console.log(res)

    

NOTE: I included a logical OR to provide a fallback case, but this is not defined in OP request

CodePudding user response:

I think you just need to sort the first array by the uniqueness of the identifier in the second array

const Array1 = [{id: '001',school: "blue springs"},{id: '002',school: "sycamore hills"},{id: '003',school: "moreland ridge"},{id: '004',school: "grain valley"}];

const Array2 = [{id: '003',participant: "Susan"},{id: '001',participant: "Henry"},{id: '003',participant: "Justin"},{id: '004',participant: "Jessica"},{id: '002',participant: "Carly"},{id: '001',participant: "Chloe" }];

const idOrder = Array2.map(({ id }) => id).filter((v, i, a) => a.indexOf(v)===i);
console.log('Order:', idOrder );

const sortedByOrder = Array1.sort(({ id: id1 }, { id: id2 }) => 
  idOrder.indexOf(id1) - idOrder.indexOf(id2));

console.log('Result:', sortedByOrder);
.as-console-wrapper{min-height: 100%!important; top: 0}

  • Related