`
sohighthesky
  • 浏览: 35773 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

New in JavaScript(Array)

阅读更多
New in JavaScript 1.8.1(Firefox3.5)

Object.getPrototypeOf()
This new method returns the prototype of a specified object.

Using native JSON
Firefox 3.5 has native support for JSON.

New trim methods on the String object
The String object now has trim(), trimLeft(), and trimRight() methods.
//fastest trim http://lifesinger.org/blog/2010/01/fastest-javascript-trim/
if(!String.prototype.trim){
    String.prototype.trim=function() {
        var s=this,whitespace = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000',
        i = 0, j = s.length - 1;
        while (i < s.length && whitespace.indexOf(s.charAt(i)) != -1) i++;
        while (j > i && whitespace.indexOf(s.charAt(j)) != -1) j--;
        return s.substring(i, j + 1);
    }
}


New in JavaScript 1.6
Array extras
There are seven new Array methods that can be separated into two categories, item location methods and iterative methods.
The item location methods are:
indexOf() - returns the index of the given item's first occurrence.
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

lastIndexOf() - returns the index of the given item's last occurrence.
if (!Array.prototype.lastIndexOf)
{
  Array.prototype.lastIndexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]);
    if (isNaN(from))
    {
      from = len - 1;
    }
    else
    {
      from = (from < 0)
           ? Math.ceil(from)
           : Math.floor(from);
      if (from < 0)
        from += len;
      else if (from >= len)
        from = len - 1;
    }

    for (; from > -1; from--)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


The iterative methods are:
every() - runs a function on items in the array while that function is returning true. It returns true if the function returns true for every item it could visit.
if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }

    return true;
  };
}

filter() - runs a function on every item in the array and returns an array of all items for which the function returns true.
if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = [];
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

forEach() - runs a function on every item in the array.
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

map() - runs a function on every item in the array and returns the results in an array.
if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }

    return res;
  };
}

some() - runs a function on items in the array while that function returns false. It returns true if the function returns true for any item it could visit.
if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp*/)
  {
    var i = 0,
        len = this.length >>> 0;

    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (; i < len; i++)
    {
      if (i in this &&
          fun.call(thisp, this[i], i, this))
        return true;
    }

    return false;
  };
}

For more information see Working with Arrays


New in JavaScript 1.8
JavaScript 1.8 is part of Gecko 1.9 (which is incorporated into Firefox 3).
Array extras
There are two new iterative Array methods included in JavaScript 1.8, specifically:

reduce() - runs a function on every item in the array and collects the results from previous calls.
if (!Array.prototype.reduce)
{
  Array.prototype.reduce = function(fun /*, initial*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    // no value to return if no initial value and an empty array
    if (len == 0 && arguments.length == 1)
      throw new TypeError();

    var i = 0;
    if (arguments.length >= 2)
    {
      var rv = arguments[1];
    }
    else
    {
      do
      {
        if (i in this)
        {
          var rv = this[i++];
          break;
        }

        // if array contains no values, no initial value to return
        if (++i >= len)
          throw new TypeError();
      }
      while (true);
    }

    for (; i < len; i++)
    {
      if (i in this)
        rv = fun.call(null, rv, this[i], i, this);
    }

    return rv;
  };
}

reduceRight() - runs a function on every item in the array and collects the results from previous calls, but in reverse.
if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(fun /*, initial*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    // no value to return if no initial value, empty array
    if (len == 0 && arguments.length == 1)
      throw new TypeError();

    var i = len - 1;
    if (arguments.length >= 2)
    {
      var rv = arguments[1];
    }
    else
    {
      do
      {
        if (i in this)
        {
          var rv = this[i--];
          break;
        }

        // if array contains no values, no initial value to return
        if (--i < 0)
          throw new TypeError();
      }
      while (true);
    }

    for (; i >= 0; i--)
    {
      if (i in this)
        rv = fun.call(null, rv, this[i], i, this);
    }

    return rv;
  };
}
分享到:
评论

相关推荐

    javascript模拟php函数in_array

    Array.prototype.in_array=function(e){ var r=new RegExp(this.S+e+this.S); return (r.test(this.S+this.join(this.S)+this.S)); }; 用法如下: var arr=new Array(["b",2,"a",4,"test"]); arr.in_array('...

    PHP函数in_array()使用详解

    在JavaScript中,你可以使用`Array.prototype.includes()`来检查数组中是否包含某个元素,这类似于PHP的`in_array()`。 总之,`in_array()`是PHP中处理数组查询的强大工具,理解其工作原理和参数设置对于优化代码和...

    javaScript array(数组)使用字符串作为数组下标的方法

    创建一个JavaScript数组通常使用`new Array()`或直接用方括号`[]`来初始化。例如: ```javascript var array = new Array(); // 或者 var array = []; ``` 然后,你可以使用字符串作为下标来设置或获取数组中的...

    用js实现in_array的方法

    此外,示例还展示了如何给JavaScript的基本数据类型(如字符串和数字)添加原型方法,使得可以在任何字符串或数字上调用`in_array`方法。 ```javascript var in_array = function(arr) { var isArr = arr && ...

    类似php的js数组的in_array函数自定义方法

    在JavaScript中,`in_array`函数是一个在PHP中广泛使用的数组操作函数,它用于检查数组中是否存在指定的元素。然而,JavaScript的原生数组对象并没有提供这样的功能。因此,为了实现类似PHP的`in_array`功能,我们...

    Javascript数组Array基础介绍_.docx

    Javascript 数组 Array 基础介绍 在 JavaScript 中,数组是一种特殊的对象,它们拥有独特的特性。在介绍数组之前,我们需要了解什么是数组。数组是一种类数组的对象,它们拥有对象的特性。当属性名是小而连续的整数...

    JavaScript in 10 Minutes

    ### JavaScript in 10 Minutes: Key Insights for Intermediate and Advanced Programmers #### Introduction "JavaScript in 10 Minutes" is a concise guide that aims to provide intermediate to advanced ...

    Professional JavaScript for Web Developers英文版

    JavaScript use with HTML to create dynamic webpages, language concepts including syntax and flow control statementsvariable handling given their loosely typed naturebuilt-in reference types such as ...

    jQuery中inArray方法注意事项分析

    然而,正如标题和描述中提到的,使用`$.inArray()`时需要注意一些事项,因为JavaScript的弱类型特性可能会导致意外的结果。 `$.inArray(element, array)`方法接收两个参数:`element`是要查找的元素,`array`是待...

    Array, Array Constructor, for in loop, typeof, instanceOf

    避免使用`for in`循环遍历数组,采用经典`for`循环并缓存`length`,明确理解`Array`构造函数的行为,以及正确使用`typeof`和`instanceOf`进行类型检查,这些都是编写高质量JavaScript代码的关键。

    JavaScript中循环遍历Array与Map的方法小结_.docx

    在JavaScript中,循环遍历Array和Map是常见的操作,尤其对于数据处理和对象操作至关重要。以下是对各种遍历方法的详细说明: 1. **基本for循环**(eg1、eg5): 基本的for循环是最基础的遍历方式,适用于任何具有...

    Javascript中Array用法实例分析

    还可以使用构造函数`new Array()`创建数组,如`var arr = new Array(10);`,这样做会创建一个长度为10的空数组。但在实际编程中,数组字面量用法更直观、简洁。 数组的遍历通常使用for循环或for...in循环。for循环...

    Javascript数组Array基础介绍

    JavaScript中的数组Array是编程中非常基础且重要的概念,尤其在JavaScript这种动态类型的脚本语言中,数组具有灵活性和广泛的应用场景。本文主要介绍了Array的基本知识,让我们深入了解一下JavaScript数组的特性。 ...

    浅析JavaScript Array和string的转换(推荐)

    var aValues = new Array();  如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20);  ——————如下2种定义方式是一样的——–1——- var aColors = new Array(); aColors[0] = red; ...

    JavaScript Cookbook

    Work with JavaScript objects, such as String, Array, Number, and Math Use JavaScript with Scalable Vector Graphics (SVG) and the canvas element Store data in various ways, from the simple to the ...

    Javascript简易70实例.doc

    `for(x in newarray)`循环用于遍历数组的所有索引,而`for(i=0; i; i++)`则用于在数组中填充或修改元素。 数组的清空可以通过重新定义数组实现,例如`myarray = new array()`。同时,我们还可以为数组对象自定义...

    JavaScript语言参考手册

    这一章包含了 JavaScript 的核心对象 Array,Boolean,Date,Function,Math,Number,Object 和 String。这些对象同时在客户端和服务器端的 JavaScript 中使用。 Array 属性 方法 Boolean 属性 方法 Date 属性 方法...

    javascript中判断一个值是否在数组中并没有直接使用

    总的来说,虽然JavaScript没有内置的`in_array()`函数,但通过自定义函数或者利用现有的`Array.prototype.includes`和`Array.prototype.indexOf`方法,我们可以轻松地实现相同的功能。同时,扩展基本类型可以提供更...

    javascript入门和基础知识

    var a = new Array(1, 2, 3); // 获取数组元素 var firstElement = a[0]; // 遍历数组 for (var i = 0; i ; i++) { console.log(a[i]); } for (var index in a) { console.log(index); // 输出索引 } ``` ####...

Global site tag (gtag.js) - Google Analytics