Load the rest countries’ data using your HTML and script.js file and run a for loop on the data and print all the country names in the console.
Give a write-up on the Difference between copy by value and copy by reference.
How to copy by value a composite datatype (array+objects).
https://restcountries.eu/rest/v2/all HTTP end point
1 | request.onload = function() { |
1 | request.onload = function() { |
There are 3 ways to copy by value for composite data types.
(...) operatorSpread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 values are expected. It allows us the privilege to obtain a list of parameters from an array.Using spread will clone your object. Note this will be a shallow copy.
1 | let A= [1, 2, 3]; |
Object.assign() methodThe
Object.assign()method copies all enumerable own properties from one or more source objects to a target object. It returns the target object. Note this will be a shallow copy.
1 | let A= [1, 2, 3]; |
JSON.stringify() and JSON.parse() methodsThe JSON object, available in all modern browsers, has two useful methods to deal with JSON-formatted content: parse and stringify.
JSON.parse()takes a JSON string and transforms it into a JavaScript object.JSON.stringify()takes a JavaScript object and transforms it into a JSON string.UsingJSON.parse()andJSON.stringify()for copy performs deep copy.
1 | let A= [1, 2, 3]; |