Javascript:将对象数组过滤为两个
我的数组对象如下例所示Javascript:将对象数组过滤为两个,javascript,arrays,sorting,filter,grouping,Javascript,Arrays,Sorting,Filter,Grouping,我的数组对象如下例所示 list = [ {'name': 'test1', 'email': 'test1@gmail.com', 'ispin': true, 'updatedAt': 1540456416646} {'name': 'test2', 'email': 'test2@gmail.com', 'ispin': false, 'updatedAt': 1540456416111} {'name': 'test3', 'email': 'test3@gmail.com', 'isp
list = [
{'name': 'test1', 'email': 'test1@gmail.com', 'ispin': true, 'updatedAt': 1540456416646}
{'name': 'test2', 'email': 'test2@gmail.com', 'ispin': false, 'updatedAt': 1540456416111}
{'name': 'test3', 'email': 'test3@gmail.com', 'ispin': true, 'updatedAt': 1540456412541}
{'name': 'test4', 'email': 'test4@gmail.com', 'ispin': false, 'updatedAt': 1540456414521}
]
我可以像这样对数组对象进行排序
return conversationList.sort((a, b) => {
const firstTimestamp = a.lastMessage && a.lastMessage.createdAt ? a.lastMessage.createdAt : 0;
const secondTimestamp = b.lastMessage && b.lastMessage.createdAt ? b.lastMessage.createdAt : 0;
//Sort in DESC order
return secondTimestamp - firstTimestamp;
});
{
// first array object with key pin : filter by ispin:true and order by descending order
pin: [
{'name': 'test1', 'email': 'test1@gmail.com', 'ispin': true, 'updatedAt': 1540456416646}
{'name': 'test3', 'email': 'test3@gmail.com', 'ispin': true, 'updatedAt': 1540456412541}
],
// second array object with ket recent : order by descending order and records which has ispin false
recent: [
{'name': 'test2', 'email': 'test2@gmail.com', 'ispin': false, 'updatedAt': 1540456416111}
{'name': 'test4', 'email': 'test4@gmail.com', 'ispin': false, 'updatedAt': 1540456414521}
]
}
使用上面的代码,我能够按降序对对象数组进行排序
现在,我的要求是第一个基于ispin键的过滤数组。如果ispin设置为true,那么我想用keypin以降序创建新的数组对象,并用keyrecent以降序再创建一个数组
所以我的最终输出应该是这样的
return conversationList.sort((a, b) => {
const firstTimestamp = a.lastMessage && a.lastMessage.createdAt ? a.lastMessage.createdAt : 0;
const secondTimestamp = b.lastMessage && b.lastMessage.createdAt ? b.lastMessage.createdAt : 0;
//Sort in DESC order
return secondTimestamp - firstTimestamp;
});
{
// first array object with key pin : filter by ispin:true and order by descending order
pin: [
{'name': 'test1', 'email': 'test1@gmail.com', 'ispin': true, 'updatedAt': 1540456416646}
{'name': 'test3', 'email': 'test3@gmail.com', 'ispin': true, 'updatedAt': 1540456412541}
],
// second array object with ket recent : order by descending order and records which has ispin false
recent: [
{'name': 'test2', 'email': 'test2@gmail.com', 'ispin': false, 'updatedAt': 1540456416111}
{'name': 'test4', 'email': 'test4@gmail.com', 'ispin': false, 'updatedAt': 1540456414521}
]
}
要根据isPin值拆分列表,可以执行如下分组:
列表=[
{'name':'test1','email':'test1@gmail.com“,'ispin':true,'updatedAt':1540456416646},
{'name':'test2','email':'test2@gmail.com','ispin':false,'updatedAt':1540456416111},
{'name':'test3','email':'test3@gmail.com“,'ispin':true,'updatedAt':1540456412541},
{'name':'test4','email':'test4@gmail.com“,'ispin':false,'updatedAt':1540456414521},
]
constgroupby=(数组,fn)=>array.reduce((结果,项)=>{
常数键=fn(项目);
如果(!result[key])result[key]=[];
结果[键]。推送(项);
返回结果;
}, {});
const result=groupBy(列表,x=>x.ispin?'pin':'recent');
控制台日志(结果)
我相信您可以通过.reduce()
方法实现这一点。您可以创建一个新数组,其中包含具有isPin:true
的对象,然后像前面一样对该数组进行排序。@T.J.Crowder:我认为这个重复的数组不好。我正在写一个答案,当你用一个groupBy特性(使用reduce作为另一个注释)来结束这个问题时。OP需要在这里分组,而不是筛选,对于这个问题,可以有比重复目标更好的答案。啊,对了,我认为这没有意义(只是在看我的答案时意识到了这一点)。它应该是{pin:[{}]}