diff --git a/Assigment-7/JS_Fundamentals.js b/Assigment-7/JS_Fundamentals.js new file mode 100644 index 0000000..ee7c769 --- /dev/null +++ b/Assigment-7/JS_Fundamentals.js @@ -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')) + + + + + + + + + diff --git a/Assigment-7/JS_Fundamentals2.js b/Assigment-7/JS_Fundamentals2.js new file mode 100644 index 0000000..a385479 --- /dev/null +++ b/Assigment-7/JS_Fundamentals2.js @@ -0,0 +1,94 @@ +//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”, ...} +//] +//} + + +// Previous Code +// 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 +// },{}) + +// } +// console.log(groupBy(users,"Male")) + +// Recent Code + +function groubByGender(users,gender){ + return users.reduce((Result,currentItem)=>{ + let KeyValue=currentItem[gender] + if(!Result[KeyValue]){ + Result[KeyValue]=[] + } + Result[KeyValue].push(currentItem) + return Result + },{}) +} + +console.log(groubByGender(users,"gender")) + + +// By using for loop + +function groupByGen(users,gender){ +let obj={} + for(let i=0;i{ + const aValue=a1[key] + const bValue=b1[key] + if(order==="asc"){ + return (aValuebValue)?1:0 + } + else if(order==="desc"){ + return (aValuebValue)? -1:0 + } + else{ + return "order is invalid" + } + }) + +return sortedUser +} +console.log(sortBy(users,"email","desc")) \ No newline at end of file diff --git a/Assigment-7/JS_Fundamentals4.js b/Assigment-7/JS_Fundamentals4.js new file mode 100644 index 0000000..a4810d6 --- /dev/null +++ b/Assigment-7/JS_Fundamentals4.js @@ -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)) + \ No newline at end of file diff --git a/Assigment-7/JS_Fundamentals5.js b/Assigment-7/JS_Fundamentals5.js new file mode 100644 index 0000000..efe2dba --- /dev/null +++ b/Assigment-7/JS_Fundamentals5.js @@ -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{ + + return typeof(value)!='object' +}) +) +} + +console.log(filterObject(obj1)) diff --git a/Assigment-7/index.html b/Assigment-7/index.html new file mode 100644 index 0000000..1e610ce --- /dev/null +++ b/Assigment-7/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + + + + \ No newline at end of file