How to extend an existing JavaScript array with another array, without creating a new array

The .push method can take multiple arguments. You can use the spread operator to pass all the elements of the second array as arguments to .push: >>> a.push(…b) If your browser does not support ECMAScript 6, you can use .apply instead: >>> a.push.apply(a, b) Or perhaps, if you think it’s clearer: >>> Array.prototype.push.apply(a,b) Please note … Read more

“Notice: Undefined variable”, “Notice: Undefined index”, “Warning: Undefined array key”, and “Notice: Undefined offset” using PHP

Notice / Warning: Undefined variable From the vast wisdom of the PHP Manual: Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued … Read more

How to Sort a Multi-dimensional Array by Value

Try a usort. If you are still on PHP 5.2 or earlier, you’ll have to define a sorting function first: function sortByOrder($a, $b) { return $a[‘order’] – $b[‘order’]; } usort($myArray, ‘sortByOrder’); Starting in PHP 5.3, you can use an anonymous function: usort($myArray, function($a, $b) { return $a[‘order’] – $b[‘order’]; }); And finally with PHP 7 … Read more

How to find the sum of an array of numbers

This’d be exactly the job for reduce. If you’re using ECMAScript 2015 (aka ECMAScript 6): const sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0); console.log(sum); // 6 For older JS: const sum = [1, 2, 3].reduce(add, 0); // with initial value to avoid when the array is empty function add(accumulator, a) { return accumulator … Read more

How to compare arrays in JavaScript?

To compare arrays, loop through them and compare every value: Comparing arrays: // Warn if overriding existing method if(Array.prototype.equals) console.warn(“Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there’s a framework conflict or you’ve got double inclusions in your code.”); // attach the .equals method to Array’s prototype to call it on any array … Read more

Deleting array elements in JavaScript – delete vs splice

delete will delete the object property, but will not reindex the array or update its length. This makes it appears as if it is undefined: > myArray = [‘a’, ‘b’, ‘c’, ‘d’] [“a”, “b”, “c”, “d”] > delete myArray[0] true > myArray[0] undefined Note that it is not in fact set to the value undefined, … Read more

Does JavaScript have a method like “range()” to generate a range within the supplied bounds?

Numbers […Array(5).keys()]; => [0, 1, 2, 3, 4] Character iteration String.fromCharCode(…[…Array(‘D’.charCodeAt(0) – ‘A’.charCodeAt(0) + 1).keys()].map(i => i + ‘A’.charCodeAt(0))); => “ABCD” Iteration for (const x of Array(5).keys()) { console.log(x, String.fromCharCode(‘A’.charCodeAt(0) + x)); } => 0,”A” 1,”B” 2,”C” 3,”D” 4,”E” As functions function range(size, startAt = 0) { return […Array(size).keys()].map(i => i + startAt); } function … Read more