Merge/flatten an array of arrays

You can use concat to merge arrays: var arrays = [ [“$6”], [“$12”], [“$25”], [“$25”], [“$18”], [“$22”], [“$10”] ]; var merged = [].concat.apply([], arrays); console.log(merged); Using the apply method of concat will just take the second parameter as an array, so the last line is identical to this: var merged2 = [].concat([“$6”], [“$12”], [“$25”], [“$25”], … Read more

Remove empty elements from an array in Javascript

A few simple ways: var arr = [1,2,,3,,-3,null,,0,,undefined,4,,4,,5,,6,,,,]; arr.filter(n => n) // [1, 2, 3, -3, 4, 4, 5, 6] arr.filter(Number) // [1, 2, 3, -3, 4, 4, 5, 6] arr.filter(Boolean) // [1, 2, 3, -3, 4, 4, 5, 6] or – (only for single array items of type “text”) [”,’1′,’2′,3,,’4′,,undefined,,,’5′].join(”).split(”); // output: [“1″,”2″,”3″,”4″,”5”] or … Read more

How do you convert a byte array to a hexadecimal string, and vice versa?

You can use Convert.ToHexString starting with .NET 5. There’s also a method for the reverse operation: Convert.FromHexString. For older versions of .NET you can either use: public static string ByteArrayToString(byte[] ba) { StringBuilder hex = new StringBuilder(ba.Length * 2); foreach (byte b in ba) hex.AppendFormat(“{0:x2}”, b); return hex.ToString(); } or: public static string ByteArrayToString(byte[] ba) … Read more

Sorting an array of objects by property values

Sort homes by price in ascending order: homes.sort(function(a, b) { return parseFloat(a.price) – parseFloat(b.price); }); Or after ES6 version: homes.sort((a, b) => parseFloat(a.price) – parseFloat(b.price)); Some documentation can be found here. For descending order, you may use homes.sort((a, b) => parseFloat(b.price) – parseFloat(a.price));

How to randomize (shuffle) a JavaScript array?

The de-facto unbiased shuffle algorithm is the Fisher-Yates (aka Knuth) Shuffle. You can see a great visualization here (and the original post linked to this) function shuffle(array) { let currentIndex = array.length, randomIndex; // While there remain elements to shuffle. while (currentIndex != 0) { // Pick a remaining element. randomIndex = Math.floor(Math.random() * currentIndex); … Read more

Get the last item in an array

if (loc_array[loc_array.length – 1] === ‘index.html’) { // do something } else { // something else } In the event that your server serves the same file for “index.html” and “inDEX.htML” you can also use: .toLowerCase(). Though, you might want to consider doing this server-side if possible: it will be cleaner and work for people … Read more

How to merge two arrays in JavaScript and de-duplicate items

To just merge the arrays (without removing duplicates) ES5 version use Array.concat: var array1 = [“Vijendra”, “Singh”]; var array2 = [“Singh”, “Shakya”]; array1 = array1.concat(array2); console.log(array1); ES6 version use destructuring const array1 = [“Vijendra”,”Singh”]; const array2 = [“Singh”, “Shakya”]; const array3 = […array1, …array2]; Since there is no ‘built in’ way to remove duplicates (ECMA-262 … Read more