0

I'm looking for the exact Javascript equivalent of php:

$tags = preg_split('/+/', $_GET['q'], NULL, PREG_SPLIT_NO_EMPTY);
$p = array_unique(array_map('strtolower', $tags));
sort($p);

It's almost done

var queryString = decodeURIComponent(location.search.substring(1)).toLowerCase(); 
var key = queryString.split('&')[0].split('=');
if (key.length > 1){ //q should be the only query key
    var tags = key[1].split(/\+/g);
    // make tags values unique, non-empty and ordered alphabetically
}

but I'm looking for 3 things that are make tags values unique, non-empty and ordered alphabetically, it's done easily in php, I hope there are such solutions in JavaScript.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • Do you know the project php.js? Here: http://phpjs.org/ – MaxArt Jun 09 '12 at 11:43
  • no but If can avoid including an additional script, would be good, If it's too hard/ long to do , your solution can be interesting –  Jun 09 '12 at 11:48
  • You don't need to include the whole script, you can freely copy its methods. You can even your own partial php.js package. – MaxArt Jun 09 '12 at 12:23

2 Answers2

3

You can find it on phpjs project:

Code:

split: (explode dependency)

function split (delimiter, string) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: explode
    // *     example 1: split(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    return this.explode(delimiter, string);
}

array_unique

function array_unique (inputArr) {
    // http://kevin.vanzonneveld.net
    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
    // +      input by: duncan
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nate
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Michael Grier
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: The second argument, sort_flags is not implemented;
    // %          note 1: also should be sorted (asort?) first according to docs
    // *     example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin']);
    // *     returns 1: {0: 'Kevin', 2: 'van', 3: 'Zonneveld'}
    // *     example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'});
    // *     returns 2: {a: 'green', 0: 'red', 1: 'blue'}
    var key = '',
        tmp_arr2 = {},
        val = '';

    var __array_search = function (needle, haystack) {
        var fkey = '';
        for (fkey in haystack) {
            if (haystack.hasOwnProperty(fkey)) {
                if ((haystack[fkey] + '') === (needle + '')) {
                    return fkey;
                }
            }
        }
        return false;
    };

    for (key in inputArr) {
        if (inputArr.hasOwnProperty(key)) {
            val = inputArr[key];
            if (false === __array_search(val, tmp_arr2)) {
                tmp_arr2[key] = val;
            }
        }
    }

    return tmp_arr2;
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • need to remove empty values also, should not be hard to slightly modify: function array_unique_non_empty( ... –  Jun 09 '12 at 12:00
  • just one other prob: .sort(); can't work on objects: TypeError: Object # has no method 'sort' –  Jun 09 '12 at 12:11
  • `preg_split` function is part of _phpjs_ project too - it can be found in workbench folder. – Václav Feb 07 '15 at 18:40
0

Since I ended up here doing a search for array_unique js equivalent I think it's worth mentioning that in ES6 you can just

var unique = (value, index, array) => array.indexOf(value) === index;

[1,1,1,2,3].filter(unique);

Or in 1 line

[1,1,1,2,3].filter((v, i, a) => a.indexOf(v) === i)
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144