How to lock or freeze an object in JavaScript

JavaScript
How to lock or freeze an object in JavaScriptThis article explains how you can prevent that anything changes to the definition of an object.Locking or freezing an object can be done with the Object.freeze(obj) method. Method descriptionThe Object.freeze() method freezes an object: it prevents new properties from being added to it. It also prevents existing properties from being removed and existing properties, or their enumerability, configurability, or writability, from being changed. The method returns the object being frozen. Important remark Object.freeze(...) is shallow, this means that object values in a frozen object are still mutable. Syntax[crayon-5a377f96a61e2839048572/] ParametersobjThe object to freeze. return valueThe frozen object. Examples This is a basic example on the use of Object.freeze[crayon-5a377f96a61f3981562828/]The following example shows that object values in a frozen object can be mutated.[crayon-5a377f96a61f9096437003/]If we want…
Read More

How to create an auto-grow directive in Angular 2

Angular 2, AngularJS
How to create an auto-grow directive in Angular 2 This article will explain how you can create a directive to automatically adjust the height of a textarea to its content. Step 1 - create the directive with angular-cli If you have not already, install angular-cli. Go to the root of your project (the folder containing "package.json"), open a shell and execute the following statements:[crayon-5a377f96a70ce067295463/]These statements will create a basic directive file and wire it into your application.[crayon-5a377f96a70d9837266860/]You can change the selector to '[autoGrow]' if you want. Step 2 - add the auto-grow code Change the code of the auto-grow directive as follows:[crayon-5a377f96a70e0825261599/]Save this file and it is ready for use. Step 3 - using the directive We are ready to use the directive. We do not need to add the…
Read More

How to remove elements from a JavaScript array based on a condition

JavaScript
How to remove elements from a JavaScript array based on a conditionYou can remove of filter one or more elements from a JavaScript array with the filter() method.The filter() method creates a new array with all elements that pass the condition implemented by the provided callback function. Syntax[crayon-5a377f96a7840535464097/] ParameterscallbackThe callback function will test each element of the array. It should return true to keep the element, false otherwise.The function takes three arguments:element The current element being processed in the array.indexThe index of the current element being processed in the array.arrayThe array filter was called upon.thisArgOptional. Value to use as this when executing callback. Return value A new array with the elements that pass the test. Examples The following example filters all elements from the array which are less than 10.[crayon-5a377f96a784b616999799/]If…
Read More

How to add an element to the end of an array in JavaScript

JavaScript
How to add an element to the end of an array in JavaScript You can add one or more elements to the end of a JavaScript array with the push() method.The method will return the new length of the array. Syntax [crayon-5a377f96a7f6c599661970/] Parameters element1 … elementNThe elements to add to the end of the array. Return value The new length property of the object upon which the method was called. Examples This is a basic example that shows how to add a single element at the end of an array.[crayon-5a377f96a7f79569128366/]You can also add multiple elements at once to an array:[crayon-5a377f96a7f7f152662319/]Adding another array with push, however, adds the array as a whole and does not add the separate elements to the array:[crayon-5a377f96a7f86765059738/]If you want to add all elements af an array…
Read More

How to add an element to the beginning of an array in JavaScript

JavaScript
How to add an element to the beginning of an array in JavaScript You can add one or more elements to the beginning of a JavaScript array with the unshift() method. The method will return the new length of the array.  Syntax [crayon-5a377f96a8646430789642/] Parameters element1 ... elementNThe elements to add to the front of the array. Return value The new length property of the object upon which the method was called. Examples This is a basic example that shows how to add a single element at the beginning of an array.[crayon-5a377f96a865e600021920/]You can also add multiple elements at once to an array:[crayon-5a377f96a8665257475179/]Adding another array with unshift, however, adds the array as a whole and does not add the separate elements to the array:[crayon-5a377f96a866b692521343/]If you want to add all elements af an…
Read More

How to concatenate arrays in JavaScript

JavaScript
How to concatenate arrays in JavaScript method description Concatenating arrays in JavaScript can be done with the concat() method.The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. syntax [crayon-5a377f96a8db5729196335/] parameters value1 ... valueNArrays and/or values to concatenate into the new array. See the examples below for details. The order of the parameters is important! First array value1 will be added, then the array value2 will be added, and so on. examples The following is a basic example where we simply merge two arrays:[crayon-5a377f96a8dc1330802739/]The previous example merged two arrays into a new one. But you can also merge single objects or a combination of arrays and objects:[crayon-5a377f96a8dc8058909670/]Interesting use-case:We have seen that we can merge arrays…
Read More

JavaScript Array.prototype.concat() Example

JavaScript
JavaScript Array.prototype.concat() Example method description The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. syntax [crayon-5a377f96a94ab971050679/] parameters value1 ... valueNArrays and/or values to concatenate into the new array. See the examples below for details. The order of the parameters is important! First the array value1 will be added, then the array value2 will be added, and so on. examples The following is a basic example where we simply merge two arrays:[crayon-5a377f96a94b7795918133/]The previous example merged two arrays into a new one. But you can also merge single objects or a combination of arrays and objects:[crayon-5a377f96a94be717063962/]Interesting use-case:We have seen that we can merge arrays and or objects. We can use this in the following situation:If you…
Read More

JavaScript Array.isArray() Example

JavaScript
JavaScript Array.isArray() method description The Array.isArray() determines whether the passed value is an Array. Show full syntax. syntax [crayon-5a377f96a9be5711924975/] parameters obj The object to be checked. return value true if the object is an Array; otherwise, false. examples All following calls return true [crayon-5a377f96a9bf1493247499/] All following calls return false [crayon-5a377f96a9bf8235759619/] polyfill Running the following code before any other code will create Array.isArray() if it's not natively available. [crayon-5a377f96a9bfe651574777/] JavaScript Array.isArray() by Mozilla Contributors is licensed under CC-BY-SA 2.5.
Read More
Angular 2 TODO List example

Angular 2 TODO List example

Angular 2, AngularJS, JavaScript, TypeScript
How to create a TODO list in Angular 2 There are a lot of examples on how to create a TODO List in Angular 2 on the web, but lots of them use outdated versions. Most of these examples use Alpha, Beta and RC versions. The problem is that they are already outdated and no longer compatible with the most recent version of Angular 2. The following example uses the 2.2.1 version. This is the most recent version at the time of writing. https://github.com/resolvethis/ng2-todo This example contains the basics about Creating components Communicating between components Creating routes Clone or download the sample code from github and start playing with the example. Check back soon for more details and explanation on this example.
Read More

How To Rename File In Java

Java
Java has a renameTo() method to rename a file.  This method is however really platform-dependent: you may successfully rename a file in *nix but failed in Windows. So, the return value (true if the file rename successful, false if failed) should always be checked to make sure the file is rename successful. File.renameTo() Example [crayon-5a377f96aa1d7660365970/]
Read More