我有以下名为的对象数组getRecommendations
。我将使用它来映射将从后端接收的数据。
const getRecommendations = [
{
recoTitle: "Retire",
recoDescription: "Its time to retire and relax with family"
},
{
recoTitle: "Buy home",
recoDescription: "Start saving to buy home in near future"
}
]
以下是我从后端收到的一系列建议,按顺序排列。例如Buy home
,Retire
在这种情况下,排名为1,排名为2。有时可能是其他方式。我要按照从后端到UI显示的接收顺序显示建议。
const recommendations = ["Buy home", "Retire"];
但是我执行以下映射,因为我不仅需要recoTitle
而且还recoDescription
显示用户。我有getRecommendations
做recoTitle和recoDescription的映射。下面的代码用于获取对象数组中的两者。如下
const filteredReco = getRecommendations.filter(
({ recoTitle}) => recoDetails && recoDetails.indexOf(recoTitle) !== -1,
);
现在,如果我filteredReco
使用控制台日志看到的内容,Retire
它将作为第一个对象,然后是Buy Home
。如何确保列表的顺序不受使用过滤器的方式的影响。有人可以建议。
recommendations.map(recName =>
getRecommendations.find(rec => rec.recoTitle === recName)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句