Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
40 changes: 40 additions & 0 deletions Assigment-7/JS_Fundamentals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Write a function mapBy to convert an array of objects into an object mapped by the specified key:

let users = [{
"id": 1,
"first_name": "Nicki",
"email": "ncrozier0@squarespace.com",
"date_of_birth": "2009/05/09"
}, {
"id": 2,
"first_name": "Raychel",
"email": "rmcgrady1@cpanel.net",
"date_of_birth": "1996/11/05"
}];
//mapBy(users, "first_name")

//This should return
//{
// “Nicki”:{id:1, first_name:”Nicki”, ...},
// “Raychel”:{id:2, first_name:”Raychell”, ...},
//}

function mapBy(users,first_name){

return users.reduce((result,item)=>{
//Putting new entry
result[item[first_name]]=item
return result
},{})
}

console.log(mapBy(users,'first_name'))









55 changes: 55 additions & 0 deletions Assigment-7/JS_Fundamentals2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//Write a functiongroupBy to convert an array of objects into groups based on the specified key:
let users = [{
"id": 1,
"first_name": "Nicki",
"email": "ncrozier0@squarespace.com",
"date_of_birth": "2009/05/09",
"gender":"Male",
}, {
"id": 2,
"first_name": "Raychel",
"email": "rmcgrady1@cpanel.net",
"date_of_birth": "1996/11/05",
"gender":"Female"
}, {
"id": 3,
"first_name": "Demetris",
"email": "dkilshall2@elpais.com",
"date_of_birth": "2018/12/31",
"gender":"Male"
}, {
"id": 4,
"first_name": "Amata",
"email": "abraiden3@canalblog.com",
"date_of_birth": "2012/05/23",
"gender":"Female"
}];
//groupBy(users, "gender")
//This should return
//{
//“Male”:[
// {id:1, first_name:”Nicki”, ...},
// {id:3, first_name:”Demetris”, ...}
//]
//“Female”:[
// {id: 2, first_name:”Raychel”, ...},
// {id: 4, first_name:”Amata”, ...}
//]
//}

function groupBy(users,gender){

return users.reduce((result,item)=>{
debugger
//Putting new entry
if(!result[gender]) {
result[gender] = [];
}
if(item.gender === gender) {
result[gender].push(item);
}
return result
},{})

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will return object for only the gender which we pass. Can you write the code which gives output as below
{
//“Male”:[
// {id:1, first_name:”Nicki”, ...},
// {id:3, first_name:”Demetris”, ...}
//]
//“Female”:[
// {id: 2, first_name:”Raychel”, ...},
// {id: 4, first_name:”Amata”, ...}
//]
//}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Amar! I have updated the code . Please have a look

console.log(groupBy(users,"Male"))
53 changes: 53 additions & 0 deletions Assigment-7/JS_Fundamentals3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Sorting an Array of Object
//Write a function that sorts an array of objects by the key that is passed in the second argument, and in the order passed as the 3rd argiment.


let users = [{
"id": 1,
"first_name": "Nicki",
"email": "ncrozier0@squarespace.com",
"date_of_birth": "2009/05/09",
"gender":"Male",
}, {
"id": 2,
"first_name": "Raychel",
"email": "rmcgrady1@cpanel.net",
"date_of_birth": "1996/11/05",
"gender":"Female"
}, {
"id": 3,
"first_name": "Demetris",
"email": "dkilshall2@elpais.com",
"date_of_birth": "2018/12/31",
"gender":"Male"
}, {
"id": 4,
"first_name": "Amata",
"email": "abraiden3@canalblog.com",
"date_of_birth": "2012/05/23",
"gender":"Female"
}]


//sort(users, “id”, “desc”) //Should return users sorted by id in descending order
//sort(users, “first_name “desc”) //Should return users sorted by first_name in ascending order


function sortBy(users,key,order){
let sortedUser=users.sort((a1,b1)=>{
const aValue=a1[key]
const bValue=b1[key]
if(order==="asc"){
return (aValue<bValue)?-1:(aValue>bValue)?1:0
}
else if(order==="desc"){
return (aValue<bValue)? 1:(aValue>bValue)? -1:0
}
else{
return "order is invalid"
}
})

return sortedUser
}
console.log(sortBy(users,"email","desc"))
60 changes: 60 additions & 0 deletions Assigment-7/JS_Fundamentals4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//Given 2 arrays with related objects, return a new array where objects having the same id from each of the arrays are merged. Try to achieve it with a complexity - O(n).


let userNames = [{
"id": 1,
"first_name": "Nicki",
}, {
"id": 2,
"first_name": "Raychel",
}, {
"id": 3,
"first_name": "Demetris",
}, {
"id": 4,
"first_name": "Amata",
}]


let userEmails = [{
"id": 2,
"email": "rmcgrady1@cpanel.net",
}, {
"id": 1,
"email": "ncrozier0@squarespace.com",
}, {
"id": 4,
"email": "abraiden3@canalblog.com",
}, {
"id": 3,
"email": "dkilshall2@elpais.com",
}]

//mergeById(userNames, userEmails)
//This should return an array of users in the format:


// Solution :

// merge botht the array of the object
function mergeById(arr1, arr2) {
const idMap = {};

// Merge objects from the first array into the map
for (const obj of arr1) {
idMap[obj.id] = { ...idMap[obj.id], ...obj };
}

// Merge objects from the second array into the map
for (const obj of arr2) {
idMap[obj.id] = { ...idMap[obj.id], ...obj };
}

// Convert the map values into an array
const result = Object.values(idMap);

return result;
}

console.log(mergeById( userNames,userEmails))

25 changes: 25 additions & 0 deletions Assigment-7/JS_Fundamentals5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//Write a function to filter an array of strings to hold only unique values
let array=["naaz","neha","priya","naaz","sili","hachiko"]


// Without using Set

function uniqueElements(array){
let uniqueArray=[]
for(let i=0;i<array.length;i++){
if(!uniqueArray.includes(array[i])){
uniqueArray.push(array[i])

}
}
return uniqueArray
}
console.log(uniqueElements(array))

// With using set

function unique(array){
let uniqueArr= new Set(array)
return uniqueArr
}
console.log(unique(array))
49 changes: 49 additions & 0 deletions Assigment-7/Question_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// How will you create a new copy of the object below while updating the value of address.details[0] to “5“?

let obj={
name:"Harry Potter",
age:12 ,
address:{
details:["4","Privet Drive"],
area:"Little Whinging",
city:"Survey",
state:"England"
}
}
// by using in built methods---Structured Clone
let newObj2= structuredClone(obj)
newObj2.address.details[0]='5'
console.log(obj)
console.log(newObj2)

/// by using spread operator

let newObj={...obj, address:{...obj.address,details:[...obj.address.details]}}
newObj.address.details[0]='6'
console.log(newObj)
console.log(obj)


//Write a function filterObj that will filter out all the keys of a flat object that have objects or arrays using Object.keys and Object.entries.

let obj1 = {
a:"Apple",
b:["Basketball","Baseball"],
c: {
call: "cellphone"
},
d: "Dog"
}

//filterObject(obj) //This should return {a:”Apple”, d:”Dog”}

function filterObject(obj1){
return Object.fromEntries(Object.entries(obj1)
.filter(([key,value])=>{

return typeof(value)!='object'
})
)
}

console.log(filterObject(obj1))
11 changes: 11 additions & 0 deletions Assigment-7/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="JS_Fundamentals2.js"></script>
</body>
</html>