- Clone the array given below
person
into a new varibale named clonedPerson
let person = [
{
input: { name: 'Ryan' },
output: { name: 'Ryan' },
},
{
input: { name: { first: 'Ryan', last: 'Haskell-Glatz' } },
output: { firstName: 'Ryan', lastName: 'Haskell-Glatz' },
},
{
input: { name: 'Ryan', age: 24 },
output: { name: 'Ryan', age: 24 },
},
{
input: {
name: { first: 'Ryan', last: 'Haskell-Glatz' },
birthday: { year: 1993, month: 'Nov' },
},
output: {
firstName: 'Ryan',
lastName: 'Haskell-Glatz',
birthdayYear: 1993,
birthdayMonth: 'Nov',
},
},
];
// Your code goes here
- Write a function named
cloneObject
that accepts an object and returns the clone of the object
function cloneObject() {
// your code
}
// Run the test below to check your function
let user = {
name: 'John',
house: 'Stark',
sisters: ['Arya', 'Sansa'],
};
let cloned = cloneObject(user);
let person = {
firstName: 'John',
lastName: 'Doe',
address: {
street: 'North 1st',
city: 'San Jose',
state: 'CA',
country: 'USA',
},
};
let clonedPerson = cloneObject(user);
console.log(
`The user object is ${
user == cloned ? `not clone` : `cloned successfully 😁👑`
}`
);
console.log(
`The person object is ${
person == clonedPerson ? `not clone` : `cloned successfully 😁👑`
}`
);
- Clone the
allBlogs
variable into a new variable named allBlogsClone
var allBlogs = {
id: 1,
title: "Alamofire JSON Serialization",
body: "All about serialization in Alamofire...",
author: {
id, 1,
fullName: "Jeff Potter",
username: "jpotts18"
},
comments: [
{
id: 1,
body: "Thanks for the help Jeff, this saved me hours"
},
{
id: 2,
body: "Your welcome. I am happy to help!"
}
]
}
- Write a function
getDeepClone
that accepts any array or object. It can be multiple level nested. You can use this article for help https://medium.com/javascript-in-plain-english/how-to-deep-copy-objects-and-arrays-in-javascript-7c911359b089
Answers: https://repl.it/@ikushaldave/js-objects-cloning-ways