我的数组对象类似于下面的示例。
[
{'email':'test@gmail.com', 'name':'abc'},
{'email':'test1@gmail.com', 'name':'bbc'},
{'email':'test2@gmail.com', 'name':'aaa'},
{'email':'test3@gmail.com', 'name':'cba'},
{'email':'test3@gmail.com', 'name':'cab'},
]
所以我的新数组将具有键 A 的键值对作为键,并且值都是以 A 字母开头的对象,等等。是如果字母 A 具有2个从 a 开始的对象,那么我还想按升序排序,如下面的最终输出示例所示
我想要的最终输出是这样的。
[
"a" : [{'email':'test@gmail.com', 'name':'aaa'},{'email':'test2@gmail.com', 'name':'abc'}],
"b" : [{'email':'test1@gmail.com', 'name':'bbc'}],
"c" : [{'email':'test3@gmail.com', 'name':'cab'},{'email':'test3@gmail.com', 'name':'cba'}]
]
答案 0 :(得分:2)
您可以对数组进行排序,然后将其分组。
String sql = "SELECT a FROM XspJobRequest a " +
"WHERE a.xspJobTypeId = :xspJobTypeId " +
"AND a.xspJobStatusId = :xspJobStatusId " +
"AND a.nextAttemptTs < (now() at time zone 'utc') " +
" ORDER BY a.insertTs";
try
{
Query query = entityManager.createQuery(sql);
query.setMaxResults(limitReturnCount);
query.setParameter("xspJobTypeId", jobType.getId());
query.setParameter("xspJobStatusId", XspJobStatusEnum.eNew.getId());
List<XspJobRequest> retval = query.getResultList();
if (!alreadyActive)
{
et.commit();
}
return retval;
}
var array = [{ email: 'test@gmail.com', name: 'abc' }, { email: 'test1@gmail.com', name: 'bbc' }, { email: 'test2@gmail.com', name: 'aaa' }, { email: 'test3@gmail.com', name: 'cba' }, { email: 'test3@gmail.com', name: 'cab' }],
grouped = array
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
.reduce((r, o) => {
var group = o.name[0].toLowerCase();
(r[group] = r[group] || []).push(o);
return r;
}, Object.create(null));
console.log(grouped);
答案 1 :(得分:2)
您可以使用reduce
方法创建对象,并可以在sort
方法内部使用名称对值进行排序。
const data = [{'email':'test@gmail.com', 'name':'Abc'},{'email':'test1@gmail.com', 'name':'bbc'},{'email':'test2@gmail.com', 'name':'aaa'},{'email':'test3@gmail.com', 'name':'cba'},{'email':'test3@gmail.com', 'name':'cab'},]
const sorted = data.reduce((r, o) => {
let key = o.name.slice(0, 1).toLowerCase();
r[key] = (r[key] || []).concat(o);
r[key].sort((a, b) => a.name.localeCompare(b.name));
return r;
}, {})
console.log(sorted)
答案 2 :(得分:1)
var array = [
{'email':'test@gmail.com', 'name':'abc'},
{'email':'test1@gmail.com', 'name':'bbc'},
{'email':'test2@gmail.com', 'name':'aaa'},
{'email':'test3@gmail.com', 'name':'cba'},
{'email':'test3@gmail.com', 'name':'cab'},
]
function getArray(array=[]){
let newObject = {};
array.forEach(i=>{
let key = i['name'].slice(0,1)
if( key && newObject[key] ){
newObject[key].push(i)
}else{
newObject[key] = Array(i)
}
})
return newObject
}
console.log(getArray(array))
答案 3 :(得分:0)
您可以使用数组精简method:
const list = [
{'email':'test@gmail.com', 'name':'abc'},
{'email':'test1@gmail.com', 'name':'bbc'},
{'email':'test2@gmail.com', 'name':'aaa'},
{'email':'test3@gmail.com', 'name':'cba'},
{'email':'test3@gmail.com', 'name':'cab'},
]
const newList = list.reduce((acc, currVal) => {
const firstLetter = currVal.name.charAt(0);
if(!acc[firstLetter]){
acc[firstLetter] = [];
}
acc[firstLetter].push(currVal);
return acc
}, {})
console.log(newList)
答案 4 :(得分:0)
循环遍历数组,基于它创建一个对象,并按填充顺序对其进行排序。
const myArr = [
{'email':'test@gmail.com', 'name':'abc'},
{'email':'test1@gmail.com', 'name':'bbc'},
{'email':'test2@gmail.com', 'name':'aaa'},
{'email':'test3@gmail.com', 'name':'cba'},
{'email':'test3@gmail.com', 'name':'cab'}
];
const finalObj = {};
function compare(a,b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}
myArr.forEach(item => {
const alph = item.name.substr(0, 1);
if (finalObj[alph]) {
finalObj[alph] = [...finalObj[alph], item].sort(compare);
} else {
finalObj[alph] = [item];
}
});
console.log(finalObj);
答案 5 :(得分:-1)
您可以使用lodash groupBy方法获得所需的结果。
var collection =[
{'email':'test@gmail.com', 'name':'abc'},
{'email':'test1@gmail.com', 'name':'bbc'},
{'email':'test2@gmail.com', 'name':'aaa'},
{'email':'test3@gmail.com', 'name':'cba'},
{'email':'test3@gmail.com', 'name':'cab'},
]
console.log(_.groupBy(collection, (item) => {
return item.name[0]
}))