The JavaScript reduce() method aggregates array elements into a single value by applying a callback function that accumulates results, enabling tasks like counting elements, building objects from arrays, and filtering data through chaining with map() and filter() methods.
深掘り
前提条件
- データがありません。
次のステップ
- データがありません。
深掘り
Assignment People Of GOT Using Reduce BJaadr in JavaScript Array for Beginners追加:
this exercise is very very similar to the previous exercise the only difference is we have to use reduce array method to solve this exercise first one is count all people so we'll define a variable called total and that will be assigned to got dot houses we will be using reduce here because input is an array and output is a number reduce accepts two parameter a callback function and the initial value because we have to count we'll start it with zero callback function gets access to accumulator and current value the first thing i normally do is to return accumulator and here we'll say accumulator is equal to accumulator plus current value is the house that has people using length property we can get the length of people and finally we can directly say return refresh it and here we can see the output 33 let's move on to the next one people buy houses so people buy a hashes add an output of array name of the house and number of people in that house so we'll use the similar code we'll just say paste and the initial value is an array and we'll change the logic here we'll say accumulator the key is the name of the house so we'll say house is coming from current value the better naming for this would be to name it house so house dot name will give me the name of the house and the value will be house dot people dot length if we refresh it will see the output in the same way it is everyone so everyone returns an array of all the people in that house so here we'll change the logic by saying accumulator is equal to accumulator dot concat will use concat to concat the value and the value we will concat is house has people and people is a array of objects so we'll use map and take out only name from people object so we'll say p dot name we'll refresh it and here we can see all the values in this way next question is name with s so here first we'll have to do a very similar thing the difference will only be when we are concatenating first we are taking out the name here we should just use filter that way we will get access to the name and will say name dot to lowercase and will say that includes s lowercase only so here if you understand we are getting house dot people let's try to see each house in that way so house dot people is this will get one object at a time each object has a name property i only need access to this so we are using map after using map we will use filter and in that way we will be able to find the output so here you can see name with ace is 18.
the other approach would directly use this function so i'll just show you that approach here in this name with a but in that we are not using reduce so that's okay so here we'll say let all people is equal to we'll call the function everyone and we'll get all the people here we just need to say return all people dot filter because we need to return a smaller size of array filter will give us name and we just need to check name dot to lower case that includes here will pass a so this function is a lot more smaller but output will be very very similar so you can do it either way so surname with s so we can do this way here and here the first thing we'll do is like name dot split with a space so the first one will always be surname and then we'll say to lower case and refresh it so it should be split okay here it is s now it is 12 so we will use this way of solving this time here we are concatenating what we'll do is like we'll filter on the basis of only surname so to get the surname we'll first say split with a space and here we'll get that get access to the first element and we'll see that it works in that way now we have people name of all houses so it is in the structure of object key is always going to be the name of the house the value is going to be in an array all the name of the people so here we'll use this kind of a function initial value is going to be an object i'm going to delete this here we'll say got houses accumulator will get access to each house inside accumulator we need to add a key with house dot name and the value will be house dot people here we'll use map to return array of same size we just need p dot name now if we refresh it here we'll see the output in this way so in this way we have completed this exercise so there are multiple ways to think about a single problem if you have to use reduce you can use it like this and here you can see how we are chaining multiple methods one after another to get our desired output
関連おすすめ
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











