I have array of object and it has in another array of obj, how to sort this array depend on equal actionId key in the inside array?
so this my original array:
const arrayOfItems = {
items: [
{
item: '1',
anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
},
{
item: '2',
anotherArray: []
},
{
item: '3',
anotherArray: []
},
{
item: '4',
anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
},
{
item: '5',
anotherArray: []
},
{
item: '6',
anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
}
]
};
the result should be all items that has the same actionId under each other
sortedArray = {
items: [
{
item: '1',
anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
},
{
item: '4',
anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
},
{
item: '6',
anotherArray: [{ actionId: '1234-dh4t-tr21-6sw8' }]
},
...
]
};
This is what I tried:
const sortingArray = arrayOfItems.items.sort((a, b) => {
return a.anotherArray > 0 && a.anotherArray[0].actionId.localeCompare(b.anotherArray[0].actionId);
})