Skip to content

Instantly share code, notes, and snippets.

@gupta-pratik
Created April 29, 2017 19:34
Show Gist options
  • Save gupta-pratik/40e55a287364331d0711d52cdd143aa7 to your computer and use it in GitHub Desktop.
Save gupta-pratik/40e55a287364331d0711d52cdd143aa7 to your computer and use it in GitHub Desktop.
Creating Immutable Objects
Creating Immutable Objects:
Here are the following ways in which you can create immutable objects:
a) Object.assign : for creating immutable object
b) concat: for creating immutable array
c) filter: The filter() method creates an array filled with all array elements that pass a test (provided as a function).
var ages = [32, 33, 16, 40];
function checkAdult(age) {
return age >= 18;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.filter(checkAdult);
}
d) map : The map() method creates a new array with the results of calling a function for every array element.
The map() method calls the provided function once for each element in an array, in order
e) reduce : The reduce() method reduces the array to a single value.
Syntax : array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment