将数组中的数据连接到对象上,通过一个中间处理程序来转换其值

3

如何最好地使用queued_Dr,通过使用all_appointments来更改其值,例如upcoming_appointments.PCD

对于这个问题,最好的方法是什么?

var queued_Dr = ["Dr.Salazar","Dr.Connors","Dr.Johnson","Dr.Pearson"]


upcoming_appointments =
[{"DOB":"01-27-2002","name":"Judy, W." ,"PCD":"Dr-S"}
    ,{"DOB":"08-15-1995","name":"John, V." ,"PCD":"Dr-C"}
    ,{"DOB":"07-05-1992","name":"David, C.","PCD":"Dr-S"}
    ,{"DOB":"01-15-2002","name":"Anna, S." ,"PCD":"Dr-J"}
    ,{"DOB":"01-15-2002","name":"Jeff, D." ,"PCD":"Dr-P"}]


all_appointments = 
[["Dr-S","New York","Dr.Salazar"],
 ["Dr-C","Austin","Dr.Connors"],
 ["Dr-J","Austin","Dr.Johnson"],
 ["Dr-S","New York","Dr.Salazar"],
 ["Dr-P","San Juan","Dr.Pearson"],
 ["Dr-J","Austin","Dr.Johnson"]]

输入:

queued_Dr = ["Dr.Salazar","Dr.Connors","Dr.Johnson","Dr.Pearson"]

期望输出:

queued_Dr = ["Dr-S","Dr-C","Dr-J","Dr-P"]

实际输出:

[ undefined, undefined, undefined, undefined ]

尝试

const mapTo = (arrayWithNames) => {
  var newArray = []; 
  return arrayWithNames.map(name => {
    const appointment = Object.values(all_appointments)
      .find(appointment => appointment[2] === name);
      newArray.push(appointment[0]);
  })
  return newArray;
}
const result = mapTo(queued_Dr)
console.log(result);

upcoming_appointments 的目的是什么?我没有看到它被引用在任何地方。此外,您提到想要“更改”一些值,但是根据您所需的输出,没有更改的数据,只是 PCD 列表。 - Ro Milton
1个回答

1
第一个解决方案是在all_appointments中查找名称并返回相应的缩写。
第二个解决方案是仅组合缩写而没有其他数组。

const queued_Dr = ["Dr.Salazar","Dr.Connors","Dr.Johnson","Dr.Pearson"];

const all_appointments = [["Dr-S","New York","Dr.Salazar"],["Dr-C","Austin","Dr.Connors"],["Dr-J","Austin","Dr.Johnson"],["Dr-S","New York","Dr.Salazar"],["Dr-P","San Juan","Dr.Pearson"],["Dr-J","Austin","Dr.Johnson"]];
 
const result1 = queued_Dr
  .map((queu) => all_appointments
    .find((appointment) => appointment.at(-1) === queu)
    .at(0));
    
console.log(result1);


const result2 = queued_Dr
  .map((queu) => {
    const [abbr, name] =  queu.split('.');
    return `${abbr}-${name.at(0)}`;
  });
  
console.log(result2);
.as-console-wrapper {max-height: 100% !important; top: 0}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接