Dhaval
Dhaval

Reputation: 1436

Js : sort and rearrange array of object

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'}]
]

Upvotes: 0

Views: 103

Answers (6)

Cata John
Cata John

Reputation: 1401

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);

Upvotes: 0

Nenad Vracar
Nenad Vracar

Reputation: 122085

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)

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386776

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; }

Upvotes: 2

abdolmajid azad
abdolmajid azad

Reputation: 35

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))

Upvotes: 1

Andr&#233; Teixeira
Andr&#233; Teixeira

Reputation: 2582

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)

Upvotes: 0

Sagar Kharche
Sagar Kharche

Reputation: 2681

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]
}))

Upvotes: -1

Related Questions