Suppose I have an array of object as,
const sampleArray = [ {name: 'Arthur',details: [ {"book":["harry pottar","lotr","something"]} ,{"book":["omega","beta","simba"]} ] },
{name: 'simmone',details: [ {"book":["ronjan","pirates"]} ,{"book":["musical","eskobar","elsa"]} ]} ]
I want output as based on total length of book, sort object.
O/P: [ {name: 'simmone',details: [ {"book":["ronjan","pirates"]} ,{"book":["musical","eskobar","elsa"]} ]},
{name: 'Arthur',details: [ {"book":["harry pottar","lotr"]} ,{"book":["omega","beta","simba"]} ]} ]
For this I calculated the total length of each book, as,
const bookDetails = sampleArray.map(i => i.book);
const emptyArray = bookDetails.map(
subarr => subarr.reduce((a, b) => a + b.book.length, 0)
);
It gives me the length of book as [6,5], on basis of book count how can I sort the object as:
O/P: [ {name: 'simmone',details: [ {"book":["ronjan","pirates"]} ,{"book":["musical","eskobar","elsa"]} ]},
{name: 'Arthur',details: [ {"book":["harry pottar","lotr"]} ,{"book":["omega","beta","simba"]} ]} ]
If anyone needs any further information, please let me know
detailsarray or thesampleArray"harry pottar","lotr", "omega","beta","simba". There are total 5 books