Nodejs Array Search searchObject(filterParams)

Here you can find the source of searchObject(filterParams)

Method Source Code

'use strict';//from w ww.ja va 2 s  . c o  m

/**
 * Ricerca di oggetti tramite parametri
 * 
 * @param filterParams
 * @returns {Array.<T>}
 */
Array.prototype.searchObject = function (filterParams) {
    return this.filter(function (v) {
        return recursiveMatchFunc(v, filterParams);
    });
};

/**
 * Ritorna un elemento casuale dell'array
 * @returns {*}
 */
Array.prototype.randomSelect = function () {
    return this[Math.floor(Math.random() * this.length)];
};

/**
 * Match ricorsivo
 * @param data
 * @param params
 * @returns {boolean}
 */
function recursiveMatchFunc(data, params) {
    var match = true;
    if (typeof params == "object" && typeof data == "object") {
        Object.keys(params).map(function (label) {
            if (data.hasOwnProperty(label)) {
                match = match && recursiveMatchFunc(data[label], params[label]);
            } else {
                match = false;
            }
        });
    } else {
        return data == params;
    }
    return match;
}

Related

  1. search(v)
    Array.prototype.search = function(v){
      for (i=0;i<this.length;i++) {
        if (this[i]===v) return i;
    
  2. search(value)
    Array.prototype.search = function(value){
      var startIndex  = 0,
      stopIndex = this.length - 1,
      middle = Math.floor((stopIndex + startIndex)/2);
      var count = 0;
      while(this[middle] != value && startIndex < stopIndex){
        if (value < this[middle]){
          stopIndex = middle - 1;
        }else if (value > this[middle]){
    ...
    
  3. search(value)
    Array.prototype.search = function(value){
      var startIndex  = 0,
      stopIndex = this.length - 1,
      middle = Math.floor((stopIndex + startIndex)/2);
      var count = 0;
      while(this[middle] != value && startIndex < stopIndex){
        count+=1;
        if (value < this[middle]){
          stopIndex = middle - 1;
    ...
    
  4. searchById(id)
    Array.prototype.searchById = function(id){
      for(var i=0; i<this.length; i++){
        if(this[i].id === id){
          return this[i];
    };
    
  5. searchIndex(n)
    "use strict";
    Array.prototype.searchIndex = function(n){
      var result = [],
          i,
          l= this.length;
      for( i= 0; i< l; i++){
        if(this[i] == n){
          result.push(i);
      if(result.length == 0){
        result.push(-1);
      return result;
    };
     var arr = [1,2,5,5,5,5,5,6,9];
    console.log(arr.searchIndex(5));
    console.log(arr.searchIndex(0));