Javascript Array first()

Description

Javascript Array first()


Array.prototype.first = function() {
  return this[0];
}

Javascript Array first()

Array.prototype.first = function(){
    if(this.length > 0)
        return this[0];
    return null;/*from   w w w . j a va  2s  .c  o m*/
};

Javascript Array first()

// keine Arrays zur Darstellung von Dictionaries
// wie im folgendem Fall verwenden. 
// Da durch prototype pollution (hinzuf?gen von zus?tzlichen
// methoden zum prototype) die funktionsweise gest?rt wird

// Array Beispiel NO-GO

var dict = new Array();

dict.alice = 32;/*from w ww .  j a  va2 s  .c  o  m*/
dict.bob = 34;
dict.chris = 62;

console.log(dict.bob);

Array.prototype.first = function() {
  return this[0];
};

Array.prototype.last = function() {
  return this[this.length - 1];
};

var names = [];
for (var name in dict) {
  names.push(name);
}

console.log(names);
console.log(names.first());
console.log(names.last());

Javascript Array first()

Array.prototype.first = function() {
  if (this[0] !== undefined)
    return this[0]
  else {/*from   www . j  av a  2  s .c  om*/
    throw new TypeError("Can't get first element of an empty array.")
  }
}

Javascript Array first()

Array.prototype.first = function(){
  if(this.length !== 0) {
    return this[0];
  }/*w ww . j  ava  2  s.  c  o m*/
  else {
    throw new TypeError("ERROR!!");
  }
};

Javascript Array first()

"use strict";//from   w w w  .j  a va 2 s  .  com

Array.prototype.first = function() {
  if(this.length > 0) {
    return this[0];
  }
  else {
    throw new TypeError("Your array has no elements!");
  }
};

Array.prototype.range = function(from, to) {
  var i = from, result = [];

  while(i <= to) {
    result.push(i);
    i++;
  }
  return result;
};

Array.prototype.sum = function() {
  return this.reduce(function(a, b) {
    return a + b;
  });
};

Array.prototype.average = function() {
  return this.sum() / this.length;
};

// console.log([].first());  //throws exception always
console.log([2].first());
console.log([].range(1, 4));
console.log([1,2,3,4,5,6,7,8,9,10].sum());
console.log([1,2].average());

Javascript Array first()

Array.prototype.first = function () {
  return this[0];
};

Array.prototype.last = function () {
  return this[this.length-1];
};

Array.prototype.empty = function () {
  return this.length === 0;
};

Array.prototype.clear = function () {
  this.length = 0;//from w  w w  .j  a v  a2  s  .  c o  m
  return this;
};

Array.prototype.size = function () {
  return this.length;
};

Array.prototype.sample = function () {
  return this[Math.floor(Math.random() * this.length)];
};

Array.prototype.compact = function () {
  return this.filter((value) => {
    return value;
  });
};

Array.prototype.include = function (object) {
  return this.filter((value) => {
    return JSON.stringify(value) === JSON.stringify(object);
  }).length > 0;
};

Array.prototype.take = function (n) {
  return this.splice(0, n);
};

Javascript Array first()

'use strict';/*from ww  w .j a  v a 2 s  .c  om*/

Array.prototype.first = function() {
  return this[0];
};

Array.prototype.last = function() {
  return this[this.length-1];
};



PreviousNext

Related