angular - Iteration error in a function -
i have stream of observable threads:
threads: observable<{ [key: string]: thread }>;
i want create function iterates every object in collection find out if id given in threadid parameter exists. if exists, function must return false.
my function not work, not know why. turns me false :
knownewthread(threadid: string): boolean { let newthread: boolean = false; this.threads .map((threaddictionary: { [key: string]: thread }) => { (let key in threaddictionary) { if (threaddictionary[key].id === threadid) { newthread = true; } } }); return newthread; }
edit :
i not know how it. inspired function allows me return thread according threadid given parameter:
getthreadfromsubscription(threadid: string): observable<thread> { return this.threads .filter((threaddictionary: { [key: string]: thread }) => { return object.keys(threaddictionary).some((key) => threaddictionary[key].id === threadid); }) .map((threaddictionary: { [key: string]: thread }) => { (let key in threaddictionary) { if (threaddictionary[key].id === threadid) { return threaddictionary[key]; } } }).first(); }
and after subscribe :
this.getthreadfromsubscription(objmessage.id) .subscribe ((thread: thread) => { objmessage.thread = thread; });
should inspired create function or different?
initializing thread variable in threadservice :
this.threads = messageservice.messages .map((messages: message[]) => { const threads: { [key: string]: thread } = {}; messages.map((message: message) => { threads[message.thread.id] = threads[message.thread.id] || message.thread; const messagesthread: thread = threads[message.thread.id]; if (!messagesthread.lastmessage || messagesthread.lastmessage.date < message.date) { messagesthread.lastmessage = message; } }); return threads; });
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
edit
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
when subscribe knownewthread function, returns list of boolean variables. outside when scoucirs function, returns single boolean variable true or false.
here wrote:
knownewthread(threadid: string): observable<boolean> { return this.threads .map((threaddictionary: { [key: string]: thread }) => { let newthread: boolean = false; (let key in threaddictionary) { if (threaddictionary[key].id === threadid) { newthread = true; } } return newthread; }).first(); } newthread: boolean = false; this.knownewthread(objmessage.id) .subscribe( (test: boolean) => { if(test === true) { this.newthread = true; } });
when id known, have variable newthread false, conversely unknown id, variable newthread true.
this should work:
knownewthread(threadid: string): observable<boolean> { return this.threads .map((threaddictionary: { [key: string]: thread }) => { let newthread: boolean = false; (let key in threaddictionary) { if (threaddictionary[key].id === threadid) { newthread = true; } } return newthread; }); }
in code
return newthread;
will executed before
for (let key in threaddictionary) { ...
because observable async.
Comments
Post a Comment