【问题标题】:find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in Array2, and make a new Array从 Array1 IN Array2 中找到第一次出现的元素,获取在 Array2 中找到元素的索引,并创建一个新数组
【发布时间】:2022-02-15 17:29:22
【问题描述】:

我有两个数组。按 id 查找,从 Array1 IN Array2 中找到第一个出现的元素,获取在 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"
    }
]

【问题讨论】:

  • 嗯,我没有看到你已经尝试过的代码in your post?
  • 请发布您的代码
  • 刚刚添加了我到目前为止的内容
  • 你希望发生什么 1) Array1 中的元素而不是 Array2 中的元素;和 2) Array2 中的元素不在 Array1 中
  • 如果 Array1 的 id 在 array2 中不存在,则忽略它

标签: javascript arrays multidimensional-array


【解决方案1】:

您首先过滤以删除 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)

    

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

【讨论】:

    【解决方案2】:

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

    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}

    【讨论】:

    • 我试过这个,唯一的问题是我不想在结果中重复,它必须是唯一标识符的第一次出现,然后创建一个新的对象数组,数组 1 将其对象移动到唯一标识符的出现与此函数匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多