JavaScript Problem: Combining and Sorting Objects in an Array

  Рет қаралды 4,880

All Things JavaScript, LLC

All Things JavaScript, LLC

Күн бұрын

Пікірлер: 20
@kittysplode
@kittysplode 2 жыл бұрын
great to see you posting!
@TillmanTech
@TillmanTech 2 жыл бұрын
This is awesome! I learn so many collateral concepts when you cover basic topics. With this video, you cover both more advanced topics and work through programming approaches to problems. This has to be the most under-appreciated channel on KZbin. Thank you Steve!
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Glad it was helpful!
@Pareshbpatel
@Pareshbpatel 2 жыл бұрын
Grouping and Sorting in JavaScript, beautifully explained. Thanks, {2022-04-15}
@vikrambam1184
@vikrambam1184 2 жыл бұрын
i am waiting for this..🙈🙈. thanks for this tutorial
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
You're welcome 😊
@sajeersayed2008
@sajeersayed2008 2 жыл бұрын
Perfect
@jsega996
@jsega996 2 жыл бұрын
Great, thanks.
@Luke-zq9qq
@Luke-zq9qq 2 жыл бұрын
Hey! This helped me through the bulk of a problem! Thanks a ton! I did have one question. Say you are dealing with data that instead of status has Ids. If you had to process a high volume of data without knowing what the ids would be in advance how would you go about setting up the initial values in the accumulator?
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
If you didn't know what the IDs would be like, you would need to use some conditionals to figure that out before hand. A bit verbose, but without a concrete example, that would be my initial suggestion.
@magnusfohlstrom
@magnusfohlstrom 2 жыл бұрын
Right now there is a chrome(101) trail started, under a flag. It's two grouping functions, Array.prototype methods groupBy() and groupByToMap(), that will be added to JavaScript core..
@magnusfohlstrom
@magnusfohlstrom 2 жыл бұрын
For the first part I do have a more dynamic solution. Instead of a switch, there are an approved list of statuses. Then the function uses the start list, to create groupes - that don't exist and add new books to the existing ones. let start = []//Array of objects. let approved = ['completed', 'in-progress', 'not-started'], //Switches all = false; //Change this one to true, if you want to output all the groups that are found in the start Array. start.reduce((acc, obj) => { let status = obj.status, //checking if there is a group by that status idx = acc.findIndex(e => e.status === status); //new book obj, that can later be added where it belongs book = {'id':obj.id,'name':obj.name}; //if group don't exist create new one idx === -1 //if flag all or approved switch are correct ? (all || approved.includes(status)) ? //adds new status object or leaves the current obj by null acc = [...acc, {'status':status, 'books':[book]}] : null //adds new book to the books key list : acc[idx].books = [...acc[idx].books, book] //updates accumulator, by returning it return acc; }, []); //initiate with empty Array instead of a predefined one.
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
Love it! Thanks for sharing!
@technologyhun609
@technologyhun609 2 жыл бұрын
Debanubce and throttle please ...
@RonClarijs
@RonClarijs Жыл бұрын
Why not replace 'map' by 'forEach' in line 59? 'map' method returns a new array which result you don't use.
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Yes, agree. forEach would be more to the point here.
@vikrambam1184
@vikrambam1184 2 жыл бұрын
can you explain this code pls : i try with : bk1.id < bk2.id ? -1 : 1 but this not work. final.map((obj) => obj.books.sort( (bk1, bk2) => bk1.name.toString().toLowerCase() < bk2.name.toString().toLowerCase() ? -1 : 1 ) )
@AllThingsJavaScript
@AllThingsJavaScript 2 жыл бұрын
It looks like it is mapping a new array and then sorting each object in that array. What is the data in final?
@RegiiPad
@RegiiPad Жыл бұрын
The following solution reals with dynamic statuses: let start = [ { id: 1, status: 'completed', name: 'The Lord of the Rings' }, { id: 2, status: 'in-progress', name: 'Lord of the Flies' }, { id: 3, status: 'not-started', name: 'Dune' }, { id: 4, status: 'not-started', name: 'American Gods' }, { id: 5, status: 'completed', name: 'Ender\'s Game' }, { id: 6, status: 'in-progress', name: 'Brave New World' }, { id: 7, status: 'completed', name: '1984' }, ]; let final = []; let hashed = start.reduce( (acc, obj) => { if (acc && !acc.hasOwnProperty(obj.status)) acc[obj.status] = []; acc[obj.status].push({ id: obj.id, name: obj.name }); return acc; }, {}); (Object.keys(hashed)).map( item => { hashed[item].sort( (bk1, bk2) => { return ((bk1.name < bk2.name) ? -1 : 1); }); final.push( {status: item, books: hashed[item]} ); }); console.log(JSON.stringify(final, null, 4));
@AllThingsJavaScript
@AllThingsJavaScript Жыл бұрын
Thanks for sharing that solution!
The Magic of the reduce Array Method
15:24
All Things JavaScript, LLC
Рет қаралды 22 М.
Deciphering JavaScript: Why Does this Recursive Function Work the Way it Does?
15:12
All Things JavaScript, LLC
Рет қаралды 1,7 М.
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
Get a Unique List of Objects in an Array of Object in JavaScript
20:35
Yagisanatode -Scott-
Рет қаралды 32 М.
JavaScript Higher Order Functions & Arrays
34:56
Traversy Media
Рет қаралды 990 М.
JS Tutorial: Find if Two Object Values are Equal to Each Other
14:24
JavaScript Question: What is a Prototype?
10:48
All Things JavaScript, LLC
Рет қаралды 39 М.
JavaScript Problem: Flattening an Array of Sub-arrays
10:10
All Things JavaScript, LLC
Рет қаралды 1,5 М.
JavaScript Tip: Locating an Object in an Array
9:40
All Things JavaScript, LLC
Рет қаралды 7 М.
Understanding JavaScript Callbacks
15:39
All Things JavaScript, LLC
Рет қаралды 115 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 820 М.
JavaScript Problem: Comparing Two Arrays without Loops
13:46
All Things JavaScript, LLC
Рет қаралды 16 М.