Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed js assignment 7 #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
34 changes: 34 additions & 0 deletions Assignment1/Q1_DeepCopyObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
1.
How will you create a new copy of the object below while updating the value of address.details[0] to “5“?
{
name:”Harry Potter”,
age: 12,
address: {
details: [“4”, “Privet Drive”],
area:”Little Whinging”,
city: “Surrey”,
state: “England”
}
}
*/

const person = {
name: "Harry Potter",
age: 12,
address: {
details: ["4", "Privet Drive"],
area: "Little Whinging",
city: "Surrey",
state: "England",
},
};

//Copying the object using structuredClone()

const copiedPerson = JSON.parse(JSON.stringify(person));

copiedPerson.address.details[0] = 5;

console.log(person);
console.log(copiedPerson);
51 changes: 51 additions & 0 deletions Assignment1/Q2_filter_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
2. 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.
Example:
let obj = {
a:”Apple”,
b:[“Basketball”,”Baseball”],
c: {
call: “cellphone”
},
d: “Dog”
}
filterObject(obj) //This should return {a:”Apple”, d:”Dog”}
*/

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

let newObj = {};
const filterObj = (obj) => {
Object.keys(obj).forEach((val) => {
if (typeof obj[val] !== "object" || obj[val] === null) {
newObj[val] = obj[val];
}
});
return newObj;
};

console.log(filterObj(obj));

// const flattenObj = (obj) => {
// let flatObject = {};
// for (const idx in obj) {
// if (typeof obj[idx] === "object") {
// const temp = flattenObj(obj[idx]);
// for (const tempIdx in temp) {
// flatObject[idx + "." + tempIdx] = temp[tempIdx];
// }
// } else {
// flatObject[idx] = obj[idx];
// }
// }
// return flatObject;
// };

// console.log(flattenObj(obj));
47 changes: 47 additions & 0 deletions Assignment2/Q1_MapBy_Object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Write a function mapBy to convert an array of objects into an object mapped by the specified key:
Example:
Let users = [{
"id": 1,
"first_name": "Nicki",
"email": "[email protected]",
"date_of_birth": "2009/05/09"
}, {
"id": 2,
"first_name": "Raychel",
"email": "[email protected]",
"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”, ...},
//}
*/

let users = [
{
id: 1,
first_name: "Nicki",
email: "[email protected]",
date_of_birth: "2009/05/09",
},
{
id: 2,
first_name: "Raychel",
email: "[email protected]",
date_of_birth: "1996/11/05",
},
];

const mapBy = (users, value) => {
const newObj = users.reduce((acc, curr) => {
acc[curr[value]] = curr;
return acc;
}, {});
return newObj;
};

console.log(mapBy(users, "first_name"));
105 changes: 105 additions & 0 deletions Assignment2/Q2_GroupBy_Object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Write a functiongroupBy to convert an array of objects into groups based on the specified key:
let users = [{
"id": 1,
"first_name": "Nicki",
"email": "[email protected]",
"date_of_birth": "2009/05/09",
“gender”:”Male”,
}, {
"id": 2,
"first_name": "Raychel",
"email": "[email protected]",
"date_of_birth": "1996/11/05",
“gender”:”Female”
}, {
"id": 3,
"first_name": "Demetris",
"email": "[email protected]",
"date_of_birth": "2018/12/31",
“gender”:”Male”
}, {
"id": 4,
"first_name": "Amata",
"email": "[email protected]",
"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”, ...}
//]
//}

*/

let users = [
{
id: 1,
first_name: "Nicki",
email: "[email protected]",
date_of_birth: "2009/05/09",
gender: "Male",
},
{
id: 2,
first_name: "Raychel",
email: "[email protected]",
date_of_birth: "1996/11/05",
gender: "Female",
},
{
id: 3,
first_name: "Demetris",
email: "[email protected]",
date_of_birth: "2018/12/31",
gender: "Male",
},
{
id: 4,
first_name: "Amata",
email: "[email protected]",
date_of_birth: "2012/05/23",
gender: "Female",
},
];

// const groupBy = (users, value) => {
// let result = {};
// let maleArr = [];
// let femaleArr = [];
// users.map((user) => {
// if (user[value] === "Male") {
// maleArr.push(user);
// } else {
// femaleArr.push(user);
// }
// });

// result["Male"] = maleArr;
// result["Female"] = femaleArr;
// return result;
// };

// console.log(groupBy(users, "gender"));

const groupBy = (users, key) => {
return users.reduce((acc, object) => {
const value = object[key];
if (acc[value]) {
acc[value].push(object);
} else {
acc[value] = [object];
}
return acc;
}, {});
};

console.log(groupBy(users, "gender"));
76 changes: 76 additions & 0 deletions Assignment2/Q3_Sort_Object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
rite 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.
Example:
let users = [{
"id": 1,
"first_name": "Nicki",
"email": "[email protected]",
"date_of_birth": "2009/05/09",
“gender”:”Male”,
}, {
"id": 2,
"first_name": "Raychel",
"email": "[email protected]",
"date_of_birth": "1996/11/05",
“gender”:”Female”
}, {
"id": 3,
"first_name": "Demetris",
"email": "[email protected]",
"date_of_birth": "2018/12/31",
“gender”:”Male”
}, {
"id": 4,
"first_name": "Amata",
"email": "[email protected]",
"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 “asc”) //Should return users sorted by first_name in ascending order

*/

let users = [
{
id: 1,
first_name: "Nicki",
email: "[email protected]",
date_of_birth: "2009/05/09",
gender: "Male",
},
{
id: 2,
first_name: "Raychel",
email: "[email protected]",
date_of_birth: "1996/11/05",
gender: "Female",
},
{
id: 3,
first_name: "Demetris",
email: "[email protected]",
date_of_birth: "2018/12/31",
gender: "Male",
},
{
id: 4,
first_name: "Amata",
email: "[email protected]",
date_of_birth: "2012/05/23",
gender: "Female",
},
];

const sort = (users, key, type) => {
users.sort((prev, next) => {
if (prev[key] > next[key]) return type === "asc" ? 1 : -1;
if (prev[key] < next[key]) return type === "asc" ? -1 : 1;
return 0;
});

return users;
};

console.log(sort(users, "id", "desc")); //Should return users sorted by id in descending order
console.log(sort(users, "first_name", "asc")); //Should return users sorted by first_name in ascending order
Loading