javascript通过特定对象属性在对象数组中查找对象
我有一个像这样的线程对象数组javascript通过特定对象属性在对象数组中查找对象,javascript,arrays,sorting,typescript,Javascript,Arrays,Sorting,Typescript,我有一个像这样的线程对象数组 threadlist:Thread[]= [thread{id:1},thread{id:2},thread{id:3},...] 我还在下面这样的用户对象中有一个ThreadRelationship数组,用于存储用户是否已将某个线程标记为书签。 请注意,线程中的id与其在数组中的位置不匹配 user{ user_id:number,...(some other user typical properties) threadRelations:Thread
threadlist:Thread[]= [thread{id:1},thread{id:2},thread{id:3},...]
我还在下面这样的用户对象中有一个ThreadRelationship数组,用于存储用户是否已将某个线程标记为书签。
请注意,线程中的id与其在数组中的位置不匹配
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
},..
]}
我想创建一个函数,它返回一个数组,其中只包含用户的书签线程。我可以使用两个for循环和if语句来实现这一点,但我认为它效率不高。。
是否有一些方法可以直接在数组中找到特定对象
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])
}
}
}
}
}
updateBookmarkedList(){
对于(var i=0;i
谢谢 使用,您只能保留书签线程关系。从这个过滤列表中,您可以根据id
标准构建线程列表
var线程=[
{id:1},
{id:2},
{id:3}
];
变量用户=[{
用户id:1,
线程关系:[
{id:2,isBookmarked:true},
{id:18,isbookmarketed:true}
]
}];
var userBookmarkedList=updateBookmarkedList(用户[0],线程);
document.body.innerHTML='Threads->'+JSON.stringify(userBookmarkedList,null,2)+';
var threadsById = new Map(threads.map(t => [t.id, t]));
函数updateBookmarkedList(用户、线程){
返回user.threadRelations.filter(函数(threadRelations){
返回threadRelation.isBookmarked===true;
}).reduce(函数(列表、线程关系){
return list.concat(threads.filter)函数(thread){
返回thread.id==threadRelation.id;
}));
}, []);
}
使用,您只能保留书签线程关系。从这个过滤列表中,您可以根据id
标准构建线程列表
var线程=[
{id:1},
{id:2},
{id:3}
];
变量用户=[{
用户id:1,
线程关系:[
{id:2,isBookmarked:true},
{id:18,isbookmarketed:true}
]
}];
var userBookmarkedList=updateBookmarkedList(用户[0],线程);
document.body.innerHTML='Threads->'+JSON.stringify(userBookmarkedList,null,2)+';
var threadsById = new Map(threads.map(t => [t.id, t]));
函数updateBookmarkedList(用户、线程){
返回user.threadRelations.filter(函数(threadRelations){
返回threadRelation.isBookmarked===true;
}).reduce(函数(列表、线程关系){
return list.concat(threads.filter)函数(thread){
返回thread.id==threadRelation.id;
}));
}, []);
}
为什么不按“线程”的id保存一张“线程”地图
要从阵列创建贴图,请执行以下操作:
if (threadsById.has(threadRelation.id)) {
...
}
要获取线程ID是否存在,请执行以下操作:
var currentThread = threadsById.get(threadRelation.id);
要通过其id获取线程,请执行以下操作:
你为什么不按“线程”的id保存一张“线程”的地图呢 要从阵列创建贴图,请执行以下操作:
if (threadsById.has(threadRelation.id)) {
...
}
要获取线程ID是否存在,请执行以下操作:
var currentThread = threadsById.get(threadRelation.id);
要通过其id获取线程,请执行以下操作:
如果
Thread.id
是唯一标识符,那么您真的应该使用Map
而不是没有键的对象的Array
。如果没有键,您将不得不在没有其他选择的情况下迭代数组
迭代数组时,请尝试使用内置的迭代方法,如forEach
。它们比线圈效率更高,看起来更整洁
为了补充,我使用当前版本的JavaScript创建了一个JSFIDLE来制作“数组映射”,如果Thread.id
不是数字,可以用Map
替换它
请参阅。如果Thread.id
是唯一标识符,那么您确实应该使用映射
,而不是使用没有键的对象的数组
。如果没有键,您将不得不在没有其他选择的情况下迭代数组
迭代数组时,请尝试使用内置的迭代方法,如forEach
。它们比线圈效率更高,看起来更整洁
为了补充,我使用当前版本的JavaScript创建了一个JSFIDLE来制作“数组映射”,如果Thread.id
不是数字,可以用Map
替换它
请参阅。使用!附带说明,JSON无效,除非这是伪代码。在这种情况下,你应该提供一个完整的例子,你有什么(用有效的数据)。附带说明,JSON无效,除非这是伪代码。在这种情况下,你应该提供一个完整的例子,你有什么(用有效的数据)。