WEB开发网
开发学院网页设计JavaScript Prototype实战教程----9.Array 阅读

Prototype实战教程----9.Array

 2010-09-14 13:10:20 来源:WEB开发网   
核心提示:<script src="prototype.js"></script><script>/**//*var arr = [1,2,3,4,8,5,4,3];// 依次输出1,2,3,4,5,4,3arr.each(function (item, index) {i

<script src="prototype.js"></script>
<script>
/**//*var arr = [1,2,3,4,8,5,4,3];
// 依次输出1,2,3,4,5,4,3
arr.each(
  function (item, index) {
    if (item < 6) {
      alert(item);
    } else {
      throw $continue;
    }
  }
);
// 依次输出1,2,3,4
arr.each(
  function (item, index) {
    if (item < 6) {
      alert(item);
    } else {
      throw $break;
    }
  }
);*/
/**//*
var arr = [1,2,3,4,5];
// arr = [1,2,3,4,5]
arr.reverse(false);
alert(arr.inspect());
// arr = [5,4,3,2,1]
arr.reverse();
alert(arr.inspect());
var arr = [1,2,3,4,5];
arr.reverse();
// 返回2在arr中的序号:1
var index = arr.indexOf(2);
alert(index)
var arr = [1,2,3,4,5];
// 不包含2和4,返回[1,3,5]
var newArr = arr.without(2,4);
alert(newArr.inspect());
var arr = [1,2,3,4,5];
arr.clear();
alert(arr[2]);
var arr = [1,2,3,4,5];
var arr2 = [2,4,6,8,10];
//[ [1,2],[2,4],[3,6],[4,8],[5,10]]
var newArr = arr.zip(arr2);
// [[1,4],[4,16],[9,36],[16,64],[25,100]]
var newArr2 = arr.zip(
  arr2,
  function (item) {
    var newItem = item.collect(
      function (item, index) {
        return item * item;
      }
    );
    return newItem;
  }
);
alert(newArr.inspect());
alert(newArr2.inspect());
var arr = [5,2,3,1,4];
// 将arr排序
var arr = arr.sortBy(
  function (item, index) {
    return item;
  }
);
arr.each(
  function (item, index) {
    alert(item);
  }
);
var arr = [
  {root: 1, square: 1},
  {root: 2, square: 4},
  {root: 3, square: 9},
  {root: 4, square: 16},
  {root: 5, square: 25}
];
var newArr = arr.pluck("square");
alert(newArr[4]);
  
var arr = [1,2,3,4,5];
var ptArr = arr.partition(
  function (item, index) {
    return item%2 == 1;
  }
);
var oddArr = ptArr[0];
var evenArr = ptArr[1];
oddArr.each(
  function (item) {
    alert(item);
  }
);
evenArr.each(
  function (item) {
    alert(item);
  }
);
var arr = [1,2,3,4,5];
// 最大值
var max = -arr.min(
  function (item, index) {
    return -item;
  }
);
// 最小值
var min = arr.min();
alert(max);
alert(min);
var arr = [1,2,3,4,5];
// 最大值
var max = arr.max();
// 最小值
var min = -arr.max(
  function (item, index) {
    return -item;
  }
);
alert(max);
alert(min);
// 求集合中每一个元素的平方
Number.prototype.square = function () {
  return this * this;
}
var arr = [1,2,3,4,5];
var newArr = arr.invoke("square");
alert(newArr[4]);
// 计算arr中所有数的乘积
var factorial = arr.inject(
  1,
  function (accumulator, item, index) {
    return accumulator * item;
  }
);
alert(factorial)
// 判断集合中是否包含”2“
var inc = arr.include(2);
alert(inc);
var arr = ["12345", "abc2", "cde", "fgh", "132ba"];
var newArray = arr.grep(
  /2/,
  function (item, index) {
    return item + " contains 2";
  }
)
newArray.each(
  function (item) {
    alert(item);
  }
);
var arr = [1,2,3,4,5];
// 返回集合中所有的奇数
var oddArr = arr.findAll(
  function (item, index) {
    return item%2 == 1;
  }
);
alert(oddArr[2]);
// 返回第一个大于3的元素
var ele = arr.find(
  function (item, index) {
    return (item > 3);
  }
);
alert(ele);
var newArr = arr.collect(
  function (item, index) {
    return item * (index + 1);
  }
);
var hasNegative = arr.any(
  function (item, index) {
    return (item < 0);
  }
);
// 测试集合中的所有元素是否大于0
var allPositive = arr.all(
  function(item, index) {
    return (item > 0);
  }
)
alert(allPositive);
arr.each(
  function (item, index) {
    alert("arr[" + index + "] = " + item);
  }
);*/
</script>

Tags:Prototype 实战 教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接