Nodejs Array Get Object getObjectBy(field, withValue)

Here you can find the source of getObjectBy(field, withValue)

Method Source Code

Array.prototype.getObjectBy = function(field, withValue)
{
   for (var i = 0; i < this.length; i++) {
      if(this[i][field] == withValue)
         return this[i];
   }//from www .j a v  a  2s  .  c o m

   return null;
}

Related

  1. getIndexByValue(value)
    Array.prototype.getIndexByValue = function (value) {
        var index = -1;
        for (var i = 0; i < this.length; i++) {
            if (this[i] == value) {
                index = i;
                break;
        return index;
    ...
    
  2. getIndexFromID(id)
    Array.prototype.getIndexFromID = function(id){
      for(var i = 0; i < this.length; i++){
        if(this[i].id == id){ return i; }
    
  3. getItemByIndex( i )
    Array.prototype.getItemByIndex = function( i ) {
      me = this;
      return me[i];
    a = [ '11', '22', '33' ];
    a.getItemByIndex( 1 );
    
  4. getItemFromID(id)
    Array.prototype.getItemFromID = function(id){
      for(var i = 0; i < this.length; i++){
        if(this[i].id == id){ return this[i]; }
    
  5. getObjHasKey(key)
    Array.prototype.getObjHasKey=function(key){
        for (var i in this){
            if(this[i].hasOwnProperty(key)){
                return i
        return false
    
  6. getObjectByKey(key, value)
    Array.prototype.getObjectByKey = function(key, value){
      var toReturn = null
      $.each(this, function(i, object){
        if(object[key] == value){
          toReturn = object
          return false
      })
      return toReturn
    ...
    
  7. getObjectKey(name, value)
    Array.prototype.getObjectKey = function(name, value) {
        var Obj = $.map(this, function(v, i) {
            return v[name] === value ? v : null;
        });
        return Obj[0];
    
  8. getObjectWithId(id)
    var fs = require("fs"),
        Server = require("net").Server;
    Array.prototype.getObjectWithId = function (id) {
      var obj = null;
      this.forEach(function (object) {
        if (object['id'] == id) {
          obj = object;
          return;
      });
      return obj;
    };
    Array.prototype.includes = function (obj) {
      return (this.indexOf(obj) != -1) ? true : false;
    };
    Server.prototype.listenToSocket = function (sockPath) {
      if (fs.existsSync(sockPath)) fs.unlinkSync(sockPath);
      this.listen(sockPath);
      fs.chmodSync(sockPath, "0777");
      return this;
    };