Conversation
Task1.js
Outdated
| function groupByKey(array, key) { | ||
| return array.reduce((hash, obj) => { | ||
| if(obj[key] === undefined) return hash; | ||
| return Object.assign(hash, { [obj[key]]:( hash[obj[key]] || [] ).concat(obj)}) |
Task1.js
Outdated
| @@ -0,0 +1,18 @@ | |||
| function groupByKey(array, key) { | |||
| return array.reduce((hash, obj) => { | |||
| if(obj[key] === undefined) return hash; | |||
There was a problem hiding this comment.
has key function or anything like this?
Task1.js
Outdated
| @@ -0,0 +1,27 @@ | |||
| function GroupByKey(array,key) | |||
| { | |||
| var tempData = {}; | |||
There was a problem hiding this comment.
Please don't use var (only use if its extremely necessary to do so.)
Task1.js
Outdated
| {id:4,name:"Rehan", city:"Lahore"}, | ||
| {id:5,name:"Saqib", city:"Karachi"}, | ||
| {id:6,name:"Farhan", city:"Islamabad"} | ||
| ]; |
There was a problem hiding this comment.
Use prettier with your VS Code
Task1.js
Outdated
| {id:6,name:"Farhan", city:"Islamabad"} | ||
| ]; | ||
|
|
||
| let key='city'; |
There was a problem hiding this comment.
should be const instead off let
Task1.js
Outdated
| { | ||
| var tempData = {}; | ||
|
|
||
| for ( var index=0; index<array.length; index++ ) |
There was a problem hiding this comment.
The logic works fine and is good.
But I want you to write another function that uses array.reduce method to do the same thing.
Task1.js
Outdated
| @@ -0,0 +1,28 @@ | |||
| function GroupByKey(array,key) | |||
| { | |||
| tempData = {}; | |||
There was a problem hiding this comment.
never declare a variable without using let or const.
Use const here as the object reference is never changed later on.
Task1.js
Outdated
| tempData[array[index][key]]=[]; | ||
|
|
||
| } | ||
| tempData[array[index][key]].push(array[index]) |
There was a problem hiding this comment.
array[index][key] this is used twice in the for loop so the sensible thing would be to assign it to a variable and then use it at both places. So later on if we need to make a change, we only need to make in 1 place.
There was a problem hiding this comment.
Ok Sir Let me resolve it
Task2.js
Outdated
| const key='city'; | ||
|
|
||
| function GroupByKey(arr,key) { | ||
| const tempData=arr.reduce((acc,cv)=>{ |
There was a problem hiding this comment.
Do we really have to create this tempData variable?
Task2.js
Outdated
| const key='city'; | ||
|
|
||
| function GroupByKey(arr,key) { | ||
| const tempData=arr.reduce((acc,cv)=>{ |
There was a problem hiding this comment.
When you take parameters make sure they are descriptive. I can't understand much by looking at this 'cv' variable.
Task2.js
Outdated
| function GroupByKey(arr,key) { | ||
| const tempData=arr.reduce((acc,cv)=>{ | ||
| if(!acc[cv[key]]){ | ||
| acc[cv[key]]=[] |
There was a problem hiding this comment.
Rehan bhai mentioned this before acc[cv[key]] can be stored as a variable because you are using it in more than on place
Task2.js
Outdated
| acc[cv[key]]=[] | ||
| } | ||
| acc[cv[key]].push(cv) | ||
| tempobj=acc |
There was a problem hiding this comment.
i cannot see the declaration of this tempObj where is this declared
No description provided.