Nodejs Array Exist exists(param, value)

Here you can find the source of exists(param, value)

Method Source Code

const DEGREE_TO_RAD = Math.PI / 180

function max(a, b) {
  return a > b ? a : b;
}

function rand(min, max) {
  if (min > max) return rand(max, min);
  if (min == max) return min;
  return min + parseInt(Math.random() * (max - min + 1));
}

// source: http://stackoverflow.com/a/105074
Math.getUniqueNumber = function() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)//from  w w w  . j  a v  a  2 s.c  om
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

// returns index if found else -1
Array.prototype.exists = function(param, value) {
  var found = -1;
  this.forEach(function(e, i) {
    if (typeof e[param] != 'undefined') {
      if (e[param] == value) {
        found = i;
      }
    }
  })
  return found;
}

Related

  1. exists(func)
    Array.prototype.exists = function(func) {
      for (var i = 0; i < this.length; i++)
        if (func(this[i]))
          return true;
      return false;
    };
    
  2. existsByKey(key, val)
    Array.prototype.existsByKey = function(key, val) {
      var exists = this.map(function(obj) { return obj[key] }).indexOf(val) != -1;
      console.log('Does \'' + val + '\' exist? ' + exists);
      return exists;