简体   繁体   English

根据 Javascript 中另一个数组的数据重新排序一个对象数组(React)

[英]Re-ordering one array of objects based on data from another array in Javascript (React)

I've been tasked with taking in an array of objects representing 'consultants' with ids for each and re-arranging them based on the most recent selected consultants of a session booking.我的任务是接收一组代表“顾问”的对象,每个对象都有 ID,并根据最近选择的 session 预订顾问重新排列它们。

So I have an array of objects of upcoming sessions with the most recent to last:所以我有一系列即将到来的会话的对象,从最近到最后:

在此处输入图像描述

And I have an array of objects of all consultants:我有一系列所有顾问的对象:

在此处输入图像描述

So therapistId of 'upcomingSessions' match id of 'consultants'所以 'upcomingSessions' 的therapistId匹配 'consultants' 的id

I wrote a method that pulls the therapists from the 'upcomingSessions' into a new array and then concats the remaining, keeping the order of the 'upcomingSessions' therapists.我编写了一种方法,将治疗师从“upcomingSessions”中拉到一个新数组中,然后连接其余的,保持“upcomingSessions”治疗师的顺序。

So the user will see the most recent selected therapists from a dropdown menu.因此,用户将从下拉菜单中看到最近选择的治疗师。

The method I wrote works but it has a nested forEach() loop because filter() only selected the used consultants but doesn't keep the order.我写的方法有效,但它有一个嵌套的forEach()循环,因为filter()只选择使用过的顾问但不保持顺序。

Here's the method:这是方法:

 const handleUpdatedConsultantList = () => {
    // first capture the ids of chosen therapists
    const consultantIds = upcomingSessions.map((us) => us.therapistId)

    // create an array of remaining therapists
    const remainingConsultants = consultants.filter(
      (c) => !consultantIds.includes(c.id),
    )

     // empty array to push in the entire object of each chosen therapist
    const recentConsultants: ConsultantType[] = []
    
     // method to push in therapists by most recent
    consultantIds.forEach((c) => {
      consultants.forEach((co) => {
        if (c === co.id) {
          recentConsultants.push(co)
        }
      })
    })

    // concat most recent with remaining
    return recentConsultants.concat(remainingConsultants)
  }

My question is, is this the best way to implement this?我的问题是,这是实现它的最佳方式吗? Nested loops always make me un-easy but maybe it's the only way for keeping the order of the selected consultants?嵌套循环总是让我感到不安,但也许这是保持所选顾问顺序的唯一方法?

This gets the filtered chosen consultants but sorts the ids from least to greatest instead of the order that was selected:这将获得过滤后的选定顾问,但将 id 从最小到最大而不是选择的顺序排序:

const selectedConsultants = consultants.filter((c) => [313, 312, 311, 302].includes(c.id))

I think you can more directly get your list of recentConsultants by using a find() lookup when mapping over the consultantIds .我认为您可以在映射consultantIds时使用find()查找更直接地获取您的recentConsultants列表。

const handleUpdatedConsultantList = () => {
  // first capture the ids of chosen therapists
  const recentConsultantIds = upcomingSessions.map((us) => us.therapistId);

  // map through ids and connect with consultant profiles
  const recentConsultants = recentConsultantIds.map((id) =>
    consultants.find((c) => c.id === id)
  );

  // create an array of remaining therapists
  const remainingConsultants = consultants.filter(
    (c) => !recentConsultantIds.includes(c.id)
  );

  // concat most recent with remaining
  return recentConsultants.concat(remainingConsultants);
};

A Map will do the job just fine: Map可以很好地完成这项工作:

 const upcomingSessions = [ {therapistId: 5}, {therapistId: 8}, {therapistId: 9}, {therapistId: 7} ]; const consultants = [ {id: 1}, {id: 2}, {id: 3}, {id: 5}, {id: 6}, {id: 7}, {id: 8}, {id: 9}, {id: 10}, {id: 11}, {id: 12} ]; const recentConsultants = new Map(upcomingSessions.map(us => [us.therapistId, ])); consultants.forEach(c => recentConsultants.set(c.id, c)); console.log([...recentConsultants.values()]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM