I have an array of thread objects like this
threadlist:Thread[]= [thread{id:1},thread{id:2},thread{id:3},...]
I also have a ThreadRelation array inside a user object like below to store if the user has a certain thread bookmarked or not. Please note that the id in Thread does not match its place in the array.
user{
user_id:number,...(some other user typical properties)
threadRelations:ThreadRelation[]=[
threadRelation{
id:2,//relates to id property of a Thread object
isBookmarked:true
},
threadRelation{
id:18,
isBookmarked:true
},..
]}
I want to create a function which returns an array with only bookmarked threads of a user. I can achieve this using two for-loops and if-statements, but i don't think its efficient.. Is there some methods where i can find that specific object inside the array directly?
updateBookmarkedList(){
for (var i = 0; i < this.activeUser.threadsRelation.length; i++){
if ( this.activeUser.threadsRelation[i].bookmarked == true){
var searchId = this.activeUser.threadsRelation[i].id;
for (var j = 0; j < this.threadlist.length; j++){
if (this.threadlist[j].id == searchId){
this.userBookmarkedList.push(this.threadlist[j])
}
}
}
}
}
Thanks!
Array.prototype.filter! A side note, your JSON is invalid, unless this is pseudo code. In which case, you should provide a full example of what you have (with valid data).