Blog Spot!


Merging arrays in javascript

If you need to merge two arrays you can use the Array.concat() function:

var array1 = [1,2,3];
var array2 = [4,5,6];
console.log(array1.concat(array2)); // [1,2,3,4,5,6];

However, this function is not the most suitable to merge large arrays because it will consume a lot of memory by creating a new array. In this case, you can use Array.push.apply(arr1, arr2) which instead will not create a new array – it will merge the second array in the first one reducing the memory usage:

var array1 = [1,2,3];
var array2 = [4,5,6];
console.log(array1.push.apply(array1, array2)); // [1,2,3,4,5,6];

Added on 08.Aug.2016
Tags: js array merge

Copy, cut and paste in vim

Cut and paste:

  1. Position the cursor where you want to begin cutting.
  2. Press v to select characters (or uppercase V to select whole lines, or Ctrl-v to select rectangular blocks).
  3. Move the cursor to the end of what you want to cut.
  4. Press d to cut (or y to copy).
  5. Move to where you would like to paste.
  6. Press P to paste before the cursor, or p to paste after.

Copy and paste is performed with the same steps except for step 4 where you would press y instead of d:

d stands for delete in Vim, which in other editors is usually called cut
y stands for yank in Vim, which in other editors is usually called copy

vim.wikia

Added on 03.Aug.2016
Tags: linux terminal vim cut copy paste

Remove Duplicates from JavaScript Array

var data = [1,2,3,4,5,6,7,1,2,3,4,5,67,8,9,4,2,3,4,5,6];

/**
 * Remove Duplicates from Array
 */
function uniqArr(arr) {
    var seen = {}, arr2 = [], i;
    for (i = 0, len = arr.length; i < len; i++) {
        if (!(arr[i] in seen)) {
            arr2.push(arr[i]);
            seen[arr[i]] = true;
        }
    }
    return arr2;
}

console.log(uniqArr(data));

stackoverflow

Added on 18.Jul.2016
Tags: js remove duplicates function

Auto delete a record in MySQL table when date is expired

You may try to use MySQL Events for that:

CREATE EVENT IF NOT EXISTS `dbName`.`eventName`
ON SCHEDULE
    EVERY 1 DAY // or 1 HOUR
COMMENT 'Description'
DO
    BEGIN

    DELETE FROM `dbName`.`TableName` WHERE `DateCol` < NOW();

    END

NOTE that MySQL Event Scheduler need to be enabled on your server:

stackoverflow

Added on 15.Jul.2016
Tags: sql mysql delete expired

Replacing all occurrences of a string in JavaScript

/**
 * Replace all occurrences of search by replacement
 */
function replaceAll(str, search, replacement) {
    return str.split(search).join(replacement);
};

Using a regular expression with the g flag set will replace all:

String.replace(/cat/g, 'dog');

The String.replace() function allows using String and Regex to replace strings, natively this function only replaces the first occurrence. But you can simulate a replaceAll() function by using the /g at the end of a Regex:

Example:

var string = "john john";
console.log(string.replace(/hn/, "ana"));  // "joana john"
console.log(string.replace(/hn/g, "ana")); // "joana joana"

stackoverflow

Added on 28.Jun.2016
Tags: js replace replaceall function

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top