0

My array object is like below example.

[
 {'email':'[email protected]', 'name':'abc'},
 {'email':'[email protected]', 'name':'bbc'},
 {'email':'[email protected]', 'name':'aaa'},
 {'email':'[email protected]', 'name':'cba'},
 {'email':'[email protected]', 'name':'cab'},
]

So my new array will have key value pare of alphabet A as a key and values are all object that's name start from A alphabet and so on.one more thing is if alphabet A has 2 objects that start from a then I also want to sort then in ascending order as I show in final output example below

final output I want is like this.

[
 "a" : [{'email':'[email protected]', 'name':'aaa'},{'email':'[email protected]', 'name':'abc'}],
 "b" : [{'email':'[email protected]', 'name':'bbc'}],
 "c" : [{'email':'[email protected]', 'name':'cab'},{'email':'[email protected]', 'name':'cba'}]
]
André Teixeira
2,6024 gold badges30 silver badges43 bronze badges
asked Oct 9, 2018 at 9:29
2
  • 4
    Have you tried anything ? Commented Oct 9, 2018 at 9:32
  • 2
    Your expected output format is not valid Commented Oct 9, 2018 at 9:33

6 Answers 6

2

You could use reduce method to create an object and inside sort method to sort values by name.

const data = [{'email':'[email protected]', 'name':'Abc'},{'email':'[email protected]', 'name':'bbc'},{'email':'[email protected]', 'name':'aaa'},{'email':'[email protected]', 'name':'cba'},{'email':'[email protected]', '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)

answered Oct 9, 2018 at 9:38

1 Comment

your code is working but one problem is if I have same alphabet in small and capital latter then it's create new key. how to stop that. so if htere is "A" or "a" I want only one key.
2

You could sort the array and then group it.

var array = [{ email: '[email protected]', name: 'abc' }, { email: '[email protected]', name: 'bbc' }, { email: '[email protected]', name: 'aaa' }, { email: '[email protected]', name: 'cba' }, { email: '[email protected]', 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);
.as-console-wrapper { max-height: 100% !important; top: 0; }

answered Oct 9, 2018 at 9:37

Comments

1

var array = [
 {'email':'[email protected]', 'name':'abc'},
 {'email':'[email protected]', 'name':'bbc'},
 {'email':'[email protected]', 'name':'aaa'},
 {'email':'[email protected]', 'name':'cba'},
 {'email':'[email protected]', '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))

answered Oct 9, 2018 at 9:42

Comments

0

You can make it using array reduce method:

const list = [
 {'email':'[email protected]', 'name':'abc'},
 {'email':'[email protected]', 'name':'bbc'},
 {'email':'[email protected]', 'name':'aaa'},
 {'email':'[email protected]', 'name':'cba'},
 {'email':'[email protected]', '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)
answered Oct 9, 2018 at 9:40

Comments

0

Loop trough the array, create an object based on it and sort it as is being filled.

const myArr = [
 {'email':'[email protected]', 'name':'abc'},
 {'email':'[email protected]', 'name':'bbc'},
 {'email':'[email protected]', 'name':'aaa'},
 {'email':'[email protected]', 'name':'cba'},
 {'email':'[email protected]', '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);

answered Oct 9, 2018 at 9:52

Comments

-1

You can use lodash groupBy method to achieve desired result.

var collection =[
 {'email':'[email protected]', 'name':'abc'},
 {'email':'[email protected]', 'name':'bbc'},
 {'email':'[email protected]', 'name':'aaa'},
 {'email':'[email protected]', 'name':'cba'},
 {'email':'[email protected]', 'name':'cab'},
]
console.log(_.groupBy(collection, (item) => {
 return item.name[0]
}))
answered Oct 9, 2018 at 9:33

1 Comment

this should be a comment imho, please provide some concrete piece of code if you want to make it an answer. Also, the question tags don't include lodash so it would be best if the proposed solution was made with vanilla

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.