HTML entities can be decoded by using several different methods involving vanilla JavaScript or JavaScript libraries. This guide will only go through vanilla JavaScript methods for decoding HTML entities as they are easy and straightforward. Show Decoding HTML Entities with the <textarea> DOM ElementThe first method is by using the textarea element. As the name suggests, the textarea element is used to create a simple text area where each character is interpreted as simple plain text.: function decode(str) { In the above code we first created the textarea element using the document.createElement() method. Then we wrote the string containing HTML entities inside the textarea using innerHTML property. This way the string will be converted to simple text and entities will be converted to characters. Lastly, we returned the string stored inside the txt variable which is the textarea. Now if we call the decode function with an HTML entity as parameter it will return it as simple text: let encodedStr = "<p>"; Decoding HTML Entities with the DOMParser.parseFromString() methodThe second method is by using the DOMParser.parseFromString() method. The DOMParser.parseFromString() method takes a string containing HTML and returns it as an HTML element: function decode(str) { In the above code we first passed the string as an argument to the DOMParser.parseFromString() method and got it back as an HTML element by specifying the second argument as “text/html”. We then returned the text content of the newly created HTML element. Now calling the decode() function: let encodedStr = "<p>"; ConclusionHTML Entities are necessary for proper viewing of text on webpages. Some websites contain code snippets as simple text. Without Entities it would be difficult to differentiate between what is a HTML code for the webpage and what is just plain text. Lodash adalah library JavaScript yang dapat membantu kita mengelola sebuah object pada JavaScript termasuk Array. Lodash memilik banyak fungsi yang bisa digunakan. Lodash juga membuat kode kita lebih bersih dan rapih. Pada artikel kali ini kita akan membahas beberapa fungsi Lodash yang dapat digunakan untuk membantu mengelola object Array pada JavaScript. Lodash: https://lodash.com/ 01. _.chunk var arrObj = _.chunk(['a', 'b', 'c', 'd'], 2); console.log(arrObj); // result => [['a', 'b'], ['c', 'd']] var arrObj = _.chunk(['a', 'b', 'c', 'd'], 3); console.log(arrObj); // result => [['a', 'b', 'c'], ['d']] 02. _.union var arrObj = _.union([2], [1, 2]); console.log(arrObj); // result => [2, 1] 03. ._uniq var arrObj = _.uniq([2, 1, 2]); console.log(arrObj); // result => [2, 1] 04. _.without var arrObj = _.without([2, 1, 2, 3], 1, 2); console.log(arrObj); // result => [3] 05. _.reverse var arrObj = _.reverse([1, 2, 3]); console.log(arrObj); // result => [3, 2, 1] Itulah beberapa fungsi Lodash yang dapat digunakan untuk mengelola object Array pada JavaScript. Untuk lebih lengkap nya dapat dilihat pada website Lodash. |