我正在使用MeteorJS(aned MongoDB)。
我有两个系列:
必须使用idEvent == idEventType
实现两个集合之间的链接。
目标是拥有一个事件数组,并关联eventstype对象。
以下代码是功能性的,但我发现它太可怕了......你怎么看?
events() {
// Type of event
const eventsType = EventsType.find();
const eventsTypeArray = [];
eventsType.forEach((ev) => {
eventsTypeArray[ev.idEventType] = ev;
});
// List of events
const eventsList = Events.find();
const eventsListArray = [];
// Merge both data
eventsList.forEach((ev) => {
const evObj = ev;
evObj.type = eventsTypeArray[ev.idEvent];
eventsListArray.push(evObj);
});
return eventsListArray;
}
谢谢! :d
答案 0 :(得分:0)
您可以map
eventsList
eventsListArray = eventsList.map(ev => Object.assign({type: eventsTypeArray[ev.idEvent]}, ev))
并使用Object.assign
来丰富原始项目:
originalArray = [{a:"1"}, {a:"2"}];
dataMap = { "1": 10, "2": 100 };
mappedArray = originalArray.map(i=>Object.assign({b:dataMap[i.a]}, i));
console.log(originalArray);
console.log(mappedArray);
试运行:
[{a:"1"}, {a:"2"}] //original array left untouched
[{a:"1", b:10}, {a:"2", b:100}] // mappedArray contains the extra data
结果:
public function loginAction(Request $request)
{
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return $this->redirectToRoute('mouvement_index');
}
$authenticationUtils = $this->get('security.authentication_utils');
return $this->render('OCUserBundle:Security:login.html.twig', array(
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
));
}
答案 1 :(得分:0)
我最近遇到了类似的问题,我希望从两个集合中加入数据。
我的解决方案是创建一个新的local collection(这是一个仅存在于客户端上的集合)。
客户:
const LocalEvents = new Mongo.Collection(null);
从那里,您可以加入它们并将新对象推送到LocalEvents
集合,而不是将已连接的对象推送到数组中。这使您能够从本地minimongo集合中查询新对象。在销毁模板/组件时,您需要确保清除本地集合。如果光标发生变化,还可以运行跟踪器功能来清空LocalCollection
。
Tracker.autorun((eventsType) => {
LocalEvents.remove({});
});