Manipulating data is core to any programming language. JavaScript is no exception, especially as JSON has token over as a prime data delivery format. One such data manipulation is reversing arrays. You may want to reverse an array to show most recent transactions, or simple alphabetic sorting.
Reversing arrays with JavaScript originally was done via reverse
but that would mutate the original array:
// First value: const arr = ['hi', 'low', 'ahhh']; // Reverse it without reassigning: arr.reverse(); // Value: arr (3) ['ahhh', 'low', 'hi']
Modifying the original array is a legacy methodology. To avoid this mutation, we’d copy the array and then reverse it:
const reversed = [...arr].reverse();
These days we can use toReversed
to avoid mutating the original array:
const arr = ['hi', 'low', 'ahhh']; const reversed = arr.toReversed(); // (3) ['ahhh', 'low', 'hi']; arr; // ['hi', 'low', 'ahhh']
Avoiding mutation of data objects is incredibly important in a programming language like JavaScript where object references are meaningful.
Serving Fonts from CDN
For maximum performance, we all know we must put our assets on CDN (another domain). Along with those assets are custom web fonts. Unfortunately custom web fonts via CDN (or any cross-domain font request) don’t work in Firefox or Internet Explorer (correctly so, by spec) though…
5 Awesome New Mozilla Technologies You’ve Never Heard Of
My trip to Mozilla Summit 2013 was incredible. I’ve spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out. MozSummit provided the perfect reminder of how brilliant my colleagues are and how much…
Create Spinning, Fading Icons with CSS3 and MooTools
A goal of my latest blog redesign was to practice what I preached a bit more; add a bit more subtle flair. One of the ways I accomplished that was by using CSS3 animations to change the display of my profile icons (RSS, GitHub, etc.) I…
Translate Content with the Google Translate API and JavaScript
Note: For this tutorial, I’m using version1 of the Google Translate API. A newer REST-based version is available. In an ideal world, all websites would have a feature that allowed the user to translate a website into their native language (or even more ideally, translation would be…
Source link