在 Array2 中找到 Array1 中第一个出现的元素,获取在 Array2 中找到该元素的索引,并创建一个新数组

find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in Array2, and make a new Array

我有两个数组。通过 id 查看,在 Array2 中找到 Array1 中第一个出现的元素,获取在 Array2 中找到该元素的索引,并创建一个新数组,其中 Array1 中的元素移动到 Array2 中找到的新索引。在 Array1 {id: 001} 中位于索引 0,我希望它位于索引 1 的新数组中(索引是在 Array2 中第一个找到的)。 {id: 002} 在 Array1 中的索引 1,我希望它在索引 3 的新数组中(索引是在 Array2 中第一个找到的)等等....

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 

预期输出

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

我认为你只需要根据第二个数组中标识符的唯一性对第一个数组进行排序

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}

您首先过滤以删除 Array2 上的重复项,然后在 Array1 中查找关于 id 的匹配项

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)

    

注意:我包含了一个逻辑或来提供后备案例,但这在 OP 请求中没有定义