-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment 7 #4
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
Open
naaz-josh
wants to merge
4
commits into
master
Choose a base branch
from
Assignment-7
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Assignment 7 #4
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| },{}) | ||
|
|
||
| } | ||
| console.log(groupBy(users,"Male")) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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”, ...}
//]
//}
There was a problem hiding this comment.
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