Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Task1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function GroupByKey(array,key)
{
const tempData = {};

for ( var index=0; index<array.length; index++)
{

if (!tempData[array[index][key]])
{
tempData[array[index][key]]=[];
}

tempData[array[index][key]].push(array[index])

}
return tempData
}
const array=[
{id:1,name:"Bilal", city:"Lahore"},
{id:1,name:"Bilal", city:"Lahore"},
{id:3,name:"Hafsa", city:"Karachi"},
{id:4,name:"Rehan", city:"Lahore"},
{id:5,name:"Saqib", city:"Karachi"},
{id:6,name:"Farhan", city:"Islamabad"}

];

const key='city';

console.log(GroupByKey(array,key))
25 changes: 25 additions & 0 deletions Task2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const array=[
{id:1,name:"Bilal", city:"Lahore"},
{id:1,name:"Bilal", city:"Lahore"},
{id:3,name:"Hafsa", city:"Karachi"},
{id:4,name:"Rehan", city:"Lahore"},
{id:5,name:"Saqib", city:"Karachi"},
{id:6,name:"Farhan", city:"Islamabad"}
];
const key='city';

function GroupByKey(arr,key) {
var tempobj=[];
arr.reduce((accumulator,currentvalue)=>{
if(!accumulator[currentvalue[key]]){
accumulator[currentvalue[key]]=[]
}
accumulator[currentvalue[key]].push(currentvalue)
tempobj=accumulator
return accumulator
},{});
return tempobj

}

console.log(GroupByKey(array,key))