From bd03d6fc35344b89f4cb579d27ccda28d4672037 Mon Sep 17 00:00:00 2001 From: PrashantSalaveJosh Date: Wed, 20 Sep 2023 12:56:17 +0530 Subject: [PATCH] completed js assignment 7 --- Assignment1/Q1_DeepCopyObject.js | 34 ++++++ Assignment1/Q2_filter_object.js | 51 +++++++++ Assignment2/Q1_MapBy_Object.js | 47 ++++++++ Assignment2/Q2_GroupBy_Object.js | 105 ++++++++++++++++++ Assignment2/Q3_Sort_Object.js | 76 +++++++++++++ Assignment2/Q4_MergeById.js | 158 +++++++++++++++++++++++++++ Assignment2/Q5_DuplicateString.js | 17 +++ Assignment3/Q1_DeepEquality_Check.js | 41 +++++++ Assignment3/Q2_DeepClone.js | 40 +++++++ Assignment3/Q3_NestedObject_Key.js | 57 ++++++++++ 10 files changed, 626 insertions(+) create mode 100644 Assignment1/Q1_DeepCopyObject.js create mode 100644 Assignment1/Q2_filter_object.js create mode 100644 Assignment2/Q1_MapBy_Object.js create mode 100644 Assignment2/Q2_GroupBy_Object.js create mode 100644 Assignment2/Q3_Sort_Object.js create mode 100644 Assignment2/Q4_MergeById.js create mode 100644 Assignment2/Q5_DuplicateString.js create mode 100644 Assignment3/Q1_DeepEquality_Check.js create mode 100644 Assignment3/Q2_DeepClone.js create mode 100644 Assignment3/Q3_NestedObject_Key.js diff --git a/Assignment1/Q1_DeepCopyObject.js b/Assignment1/Q1_DeepCopyObject.js new file mode 100644 index 0000000..ff92a5d --- /dev/null +++ b/Assignment1/Q1_DeepCopyObject.js @@ -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); diff --git a/Assignment1/Q2_filter_object.js b/Assignment1/Q2_filter_object.js new file mode 100644 index 0000000..a463013 --- /dev/null +++ b/Assignment1/Q2_filter_object.js @@ -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)); diff --git a/Assignment2/Q1_MapBy_Object.js b/Assignment2/Q1_MapBy_Object.js new file mode 100644 index 0000000..5907c49 --- /dev/null +++ b/Assignment2/Q1_MapBy_Object.js @@ -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": "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”, ...}, +//} +*/ + +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", + }, +]; + +const mapBy = (users, value) => { + const newObj = users.reduce((acc, curr) => { + acc[curr[value]] = curr; + return acc; + }, {}); + return newObj; +}; + +console.log(mapBy(users, "first_name")); diff --git a/Assignment2/Q2_GroupBy_Object.js b/Assignment2/Q2_GroupBy_Object.js new file mode 100644 index 0000000..62e1aa7 --- /dev/null +++ b/Assignment2/Q2_GroupBy_Object.js @@ -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": "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”, ...} +//] +//} + +*/ + +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", + }, +]; + +// 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")); diff --git a/Assignment2/Q3_Sort_Object.js b/Assignment2/Q3_Sort_Object.js new file mode 100644 index 0000000..932888b --- /dev/null +++ b/Assignment2/Q3_Sort_Object.js @@ -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": "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 “asc”) //Should return users sorted by first_name in ascending order + +*/ + +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", + }, +]; + +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 diff --git a/Assignment2/Q4_MergeById.js b/Assignment2/Q4_MergeById.js new file mode 100644 index 0000000..978b64b --- /dev/null +++ b/Assignment2/Q4_MergeById.js @@ -0,0 +1,158 @@ +/* +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). +Example: + +● 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: +[{ +"id": 1, +"first_name": "Nicki", +"email": "ncrozier0@squarespace.com", +}, { +"id": 2, +"first_name": "Raychel", +"email": "rmcgrady1@cpanel.net", +}, { +"id": 3, +"first_name": "Demetris", +"email": "dkilshall2@elpais.com", +}, { +"id": 4, +"first_name": "Amata", +"email": "abraiden3@canalblog.com", +}] + +*/ + +let userNames = [ + { + id: 1, + first_name: "Nicki", + }, + { + id: 2, + first_name: "Raychel", + }, + { + id: 3, + first_name: "Demetris", + }, + { + id: 4, + first_name: "Amata", + }, + { + id: 5, + first_name: "Amata", + }, +]; + +let userEmails = [ + { + id: 2, + email: "rmcgrady1@cpanel.net", + }, + { + id: 1, + email: "ncrozier0@squarespace.com", + }, + { + id: 4, + email: "abraiden3@canalblog.com", + }, +]; + +// const mergeById = (arr1, arr2) => { +// let newArr = arr1.concat(arr2); + +// let resArr = []; + +// for (let i = 0; i < newArr.length; i++) { +// let tempArr = newArr.filter((ele) => ele["id"] === newArr[i]["id"]); +// let tempObj = { ...tempArr[0], ...tempArr[1] }; +// resArr.push(tempObj); +// } + +// let res = []; +// let check = {}; + +// resArr.forEach((el) => { +// if (!check[el["id"]]) { +// res.push(el); +// check[el["id"]] = 1; +// } +// }); + +// return res; +// }; + +//console.log(mergeById(userNames, userEmails)); + +const solution = (arr1, arr2) => { + let i = 0; + let j = 0; + let result = []; + arr1.length - 1 >= i; + while (i < arr1.length && j < arr2.length) { + if (arr1[i]["id"] > arr2[j]["id"]) { + result.push(arr2[j]); + j++; + } else if (arr1[i]["id"] < arr2[j]["id"]) { + result.push(arr1[i]); + i++; + } else { + result.push({ ...arr1[i], ...arr2[j] }); + i++; + j++; + } + } + + while (i < arr1.length) { + result.push(arr1[i]); + i++; + } + + while (j < arr2.length) { + result.push(arr2[j]); + j++; + } + return result; +}; + +const sortById = (arr1) => { + arr1.sort((a, b) => a["id"] - b["id"]); + return arr1; +}; + +const sortedNames = sortById(userNames); +const sortedEmails = sortById(userEmails); +const resultArray = solution(sortedNames, sortedEmails); + +console.log(resultArray); diff --git a/Assignment2/Q5_DuplicateString.js b/Assignment2/Q5_DuplicateString.js new file mode 100644 index 0000000..54b301f --- /dev/null +++ b/Assignment2/Q5_DuplicateString.js @@ -0,0 +1,17 @@ +//Write a function to filter an array of strings to hold only unique values + +const fruits = ["apple", "mango", "gauva", "apple", "gauva"]; + +const getUniqueStringArray = (fruits) => { + const map = {}; + const res = []; + fruits.forEach((fruit) => { + if (!map[fruit]) { + res.push(fruit); + map[fruit] = 1; + } + }); + return res; +}; + +console.log(getUniqueStringArray(fruits)); diff --git a/Assignment3/Q1_DeepEquality_Check.js b/Assignment3/Q1_DeepEquality_Check.js new file mode 100644 index 0000000..3acce1f --- /dev/null +++ b/Assignment3/Q1_DeepEquality_Check.js @@ -0,0 +1,41 @@ +/* + Write a function to check deep equality of two nested objects/arrays + */ + +const isNestedObjectEqual = (obj1, obj2) => { + if (JSON.stringify(obj1) === JSON.stringify(obj2)) { + for (let key of Object.keys(obj1)) { + if (typeof obj1[key] === "object") { + if (obj1[key] !== obj2[key]) { + isNestedObjectEqual(obj1[key], obj2[key]); + } else { + return false; + } + } + } + return true; + } + return false; +}; + +const student1 = { + name: { fname: "Amar", lname: { momSurname: "kumar", dadSurname: "shetty" } }, + rollno: 21, + hobbies: ["football", "reading"], +}; + +const student2 = JSON.parse(JSON.stringify(student1)); + +const student3 = student1; + +const checkNested = (obj1, obj2) => { + if (isNestedObjectEqual(obj1, obj2)) { + console.log("Deep Copy"); + } else { + console.log("Shallow Copy"); + } +}; + +checkNested(student1, student2); + +checkNested(student1, student3); diff --git a/Assignment3/Q2_DeepClone.js b/Assignment3/Q2_DeepClone.js new file mode 100644 index 0000000..cdebe01 --- /dev/null +++ b/Assignment3/Q2_DeepClone.js @@ -0,0 +1,40 @@ +/* +Write a recursive function to create a deep clone of a nested object + */ + +const getClone = (object) => { + let tempObj = {}; + for (let key of Object.keys(object)) { + let value = object[key]; + if (typeof value === "object") { + if (Array.isArray(value)) { + value.forEach((ele) => { + if (tempObj[key]) { + tempObj[key].push(ele); + } else { + tempObj[key] = [ele]; + } + }); + } else { + tempObj[key] = getClone(value); + } + } else { + tempObj[key] = value; + } + } + return tempObj; +}; + +const student1 = { + name: { fname: "Amar", lname: "Kumar" }, + rollno: 21, + hobbies: ["football", "reading"], +}; + +const student2 = student1; + +const student3 = getClone(student1); +student3.name = "Shubh"; + +console.log(student1, student3); +console.log(student1 === getClone(student1)); diff --git a/Assignment3/Q3_NestedObject_Key.js b/Assignment3/Q3_NestedObject_Key.js new file mode 100644 index 0000000..9e7b47e --- /dev/null +++ b/Assignment3/Q3_NestedObject_Key.js @@ -0,0 +1,57 @@ +/* + Write a function that returns a nested key within an object: +Example: + +{ + name:”Harry Potter”, + age: 12, + address: { + details: [“4”, “Privet Drive”], + area:”Little Whinging”, + city: “Surrey”, + state: “England” + } +} +getNestedKey(obj, “address.details.1”) +//This should return “Privet Drive” + +*/ + +const person = { + name: "Harry Potter", + age: 12, + address: { + details: ["4", "Privet Drive"], + area: "Little Whinging", + city: "Surrey", + state: "England", + }, +}; + +const getNestedKey = (object, reqKey) => { + const flatObject = getFlatObject(object); + for (let key of Object.keys(flatObject)) { + if (key === reqKey) { + return flatObject[key]; + } + } +}; + +const getFlatObject = (object) => { + const resultObj = {}; + for (let key of Object.keys(object)) { + let value = object[key]; + if (typeof value === "object") { + const tempObj = getFlatObject(value); + for (let tempKey of Object.keys(tempObj)) { + resultObj[key + "." + tempKey] = tempObj[tempKey]; + } + } else { + resultObj[key] = value; + } + } + return resultObj; +}; + +//console.log(getFlatObject(person)); +console.log(getNestedKey(person, "address.details.1"));