官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/array.html#dojo-base-array

array模块dojo进行了很好的封装,如果想要调用必须先加载该模块:

require(["dojo/_base/array"], function(array){
// array contains the features
});

  indexOf()

返回值:字符串在数组中第一次出现的位置,如果没有找到默认返回-1.

require(["dojo/_base/array"], function(array){
array.indexOf(arrayObject, valueToFind, fromIndex, findLast);
});

  示例:

require(["dojo/_base/array", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],
function(array, dom, domConst, on){ var arrIndxOf = ["foo", "hoo", "zoo"]; on(dom.byId("refButton1"), "click", function(){
var position = array.indexOf(arrIndxOf, "zoo");
domConst.place(
"<p>The index of the word 'zoo' within the array is " + position + "</p>",
"result1",
"after"
);
}); });
<div>The content of our test array is <code>["foo", "hoo", "zoo"]</code>.</div>
<button id="refButton1" type="button">Show the index of the word 'zoo' within the array.</button>
<div id="result1"></div>

lastIndexOf()

返回值:字符串在数组中的最后位置,如果没有找到默认返回-1.

require(["dojo/_base/array"], function(array){
array.lastIndexOf(arrayObject, valueToFind, fromIndex);
});

示例:

require(["dojo/_base/array", "dojo/dom-construct", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(array, domConst, dom, on){ var arrLastIndxOf = ["foo", "hoo", "zoo", "shoe", "zoo", "nuu"]; on(dom.byId("refButton2"), "click", function(){
var position = array.lastIndexOf(arrLastIndxOf, "zoo");
domConst.place(
"<p>The last index of the word 'zoo' within the array is " + position + "</p>",
"result2",
"after"
);
});
});
<div>The content of our test array is <code>["foo", "hoo", "zoo", "shoe", "zoo", "nuu"]</code>.</div>
<button id="refButton2" type="button">Show the last index of the word 'zoo' within the array.</button>
<div id="result2"></div>

forEach()

遍历数组或者nodelists。

require(["dojo/_base/array"], function(array){
array.forEach(arrayObject, callback, thisObject);
});

第三个参数,调节forEach()的访问范围。

示例:

require(["dojo/_base/array"], function(array){
var foo = {
myMethod: function(el){
console.log(el);
}
}; array.forEach(["a","b","c"],function(item){
this.myMethod(item);
}, foo);
});

如果你想break这个循环,可以使用some()

require(["dojo/_base/array", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(array, dom, on){ on(dom.byId("start"), "click", function(){
var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
count; // iterate ALL entries of myArray
count = 0;
array.forEach(myArray, function(entry){
count++;
}); alert("iterated " + count + " entries (forEach())"); // will show "iterated 10 entries" // let's only iterate the first 4 entries of myArray
count = 0;
array.some(myArray, function(entry){
if(count >= 4){
return false;
}
count++;
}); alert("iterated "+count+" entries (some())"); // will show "iterated 4 entries"
});
});
<button id="start" type="button">Start Testloops</button>

filter()

返回值:数组过滤之后的结果数组。

require(["dojo/_base/array"], function(array){
filteredArray = array.filter(unfilteredArray, callback, thisObject);
});

示例:

require(["dojo/_base/array", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],
function(array, dom, domConst, on){
var arr = [
{ surname: "Washington", name: "Paul" },
{ surname: "Gordon", name: "Amie" },
{ surname: "Meyer", name: "Sofie" },
{ surname: "Jaysons", name: "Josh" },
{ surname: "Washington", name: "George" },
{ surname: "Doormat", name: "Amber" },
{ surname: "Smith", name: "Susan" },
{ surname: "Hill", name: "Strawberry" },
{ surname: "Washington", name: "Dan" },
{ surname: "Dojo", name: "Master" }
]; on(dom.byId("start"), "click", function(){
var filteredArr = array.filter(arr, function(item){
return item.surname == "Washington";
}); array.forEach(filteredArr, function(item, i){
domConst.create("li", {innerHTML: i+1+". "+item.surname+", "+item.name}, "filtered-items");
}); array.forEach(arr, function(item, i){
domConst.create("li", {innerHTML: i+1+". "+item.surname+", "+item.name}, "unFiltered-items");
});
});
});
<button id="start" type="button">Filter array</button>
<br/>
<div style="width: 300px; float: left;">
Filtered items<br />
(only people with "Washington" as surname)
<ul id="filtered-items"> </ul>
</div>
<div style="width: 300px; float: left;">
Unfiltered items<br />
(all people are represented in the list)
<ul id="unFiltered-items"> </ul>
</div>

map()

返回值:经过修改之后的另外一个数组。

require(["dojo/_base/array"], function(array){
array.map(arrayObject, callback, thisObject);
});

示例:

require(["dojo/_base/array", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],
function(array, dom, domConst, on){ var arrValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; on(dom.byId("button"), "click", function(){
var doubleValue = array.map(arrValues, function(item){
return item * 2;
}); array.forEach(doubleValue, function(item){
var li = domConst.create("li");
li.innerHTML = item;
dom.byId("arrValuesAfter-items").appendChild(li);
}); array.forEach(arrValues, function(item){
var li = domConst.create("li");
li.innerHTML = item;
dom.byId("arrValues-items").appendChild(li);
});
});
});
<button id="button" type="button">Run array.map()</button>
<br />
<div style="width: 300px; float: left; margin-top: 10px;">
Values before running array.map()
<ul id="arrValues-items"></ul>
</div>
<div style="width: 300px; float: left; margin-top: 10px;">
Values after running array.map()
<ul id="arrValuesAfter-items"></ul>
</div>

有时候为了不修改原数组本身,dojo提供了dojo/_base/lang::clone()

示例:

require(["dojo/_base/array", "dojo/_base/lang", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],
function(array, lang, dom, domConst, on){ var arrSalary = [
{ surname: "Washington", name: "Paul", salary: 200 },
{ surname: "Gordon", name: "Amie", salary: 350 },
{ surname: "Meyer", name: "Sofie", salary: 100 },
{ surname: "Jaysons", name: "Josh", salary: 2500 },
{ surname: "Washington", name: "George", salary: 10 },
{ surname: "Doormat", name: "Amber", salary: 320 },
{ surname: "Smith", name: "Susan", salary: 3200 },
{ surname: "Hill", name: "Strawberry", salary: 290 },
{ surname: "Washington", name: "Dan", salary: 200 },
{ surname: "Dojo", name: "Master", salary: 205 }
]; on(dom.byId("button"), "click", function(){
var raisedSalaries = array.map(arrSalary, function(item){
var newItem = lang.clone(item);
newItem.salary += (newItem.salary/100)*10;
return newItem;
}); array.forEach(raisedSalaries, function(item, i){
var li = domConst.create("li");
li.innerHTML = i+1+". "+item.surname+", "+item.name+". New salary: "+item.salary;
dom.byId("filteredSalary-items").appendChild(li);
}); array.forEach(arrSalary, function(item, i){
var li = domConst.create("li");
li.innerHTML = i+1+". "+item.surname+", "+item.name+". Old salary: "+item.salary;
dom.byId("unFilteredSalary-items").appendChild(li);
});
});
});
<button id="button" type="button">Raise the salary</button>
<br />
<div style="width: 300px; float: left; margin-top: 10px;">
Peoples' salaries after raise:
<ul id="filteredSalary-items"></ul>
</div>
<div style="width: 300px; float: left; margin-top: 10px;">
Peoples' salaries before raise:
<ul id="unFilteredSalary-items"></ul>
</div>

some()

返回值:true or  false。只要方法里有一个是真,那么其将会返回true。常常被用在if语句中。

require(["dojo/_base/array"], function(array){
var a = array.some(arrayObject, callback, thisObject);
});

示例:检查是否有一个值 >= 1,000,000。

require(["dojo/_base/array", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],
function(array, dom, domConst, on){
var arrIndxSome = [200000, 500000, 350000, 1000000, 75, 3]; on(dom.byId("refButton6"), "click", function(){
if(array.some(arrIndxSome, function(item){ return item >= 1000000; })){
result = 'yes, there are';
}else{
result = 'no, there are no such items';
} domConst.place(
"<p>The answer is: " + result + "</p>",
"result6",
"after"
);
});
});
<div>The content of our test array is <code>[200000, 500000, 350000, 1000000, 75, 3]</code>.</div>
<button id="refButton6" type="button">Are there some items >=1000000 within the array?</button>
<div id="result6"></div>

every()

返回值:true or false  。只有当方法里面,每一项都返回true,那么最终才会返回true。常常被用在if语句中。

require(["dojo/_base/array"], function(array){
array.every(arrayObject, callback, thisObject);
});

示例:检查是否每一项都>3000

require(["dojo/_base/array", "dojo/dom", "dojo/dom-construct", "dojo/on", "dojo/domReady!"],
function(array, dom, domConst, on){ var arrIndxEvery = [
{ month: "january", income: 2000 },
{ month: "february", income: 3200 },
{ month: "march", income: 2100 }
]; on(dom.byId("refButton7"), "click", function(){
if(array.every(arrIndxEvery , function(item){ return item.income >= 3000; })){
result = "yes, all income >= 3000";
}else{
result = "no, not all income >= 3000";
}
domConst.place(
"<p>The answer is: " + result + "</p>",
"result7",
"after"
);
});
});
<div>The content of our test array is <code>[{ month: "january", income: 2000 }, { month: "february", income: 3200 }, { month: "march", income: 2100 }]</code>.</div>
<button id="refButton7" type="button">Is the client allowed to get the credit?</button>
<div id="result7"></div>

dojo 官方翻译 dojo/_base/array 版本1.10的更多相关文章

  1. dojo 官方翻译 dojo/_base/lang 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang 应用加载声明: require ...

  2. dojo 官方翻译 dojo/aspect

    官网地址:http://dojotoolkit.org/reference-guide/1.10/dojo/aspect.html after() 定义:after(target, methodNam ...

  3. dojo 官方翻译 dojo/domReady 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/domReady.html#dojo-domready dom加载完成后,执行. requi ...

  4. dojo 官方翻译 dojo/json 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/json.html#dojo-json require(["dojo/json&q ...

  5. dojo 官方翻译 dojo/string 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/string.html#dojo-string require(["dojo/st ...

  6. dojo 官方翻译 dojo/Deferred

    延迟,异步调用 官网地址:http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html require(["dojo/Defer ...

  7. DOJO官方API翻译或解读-dojo/store (自定制存储器)

    dojo/store 是对已存数据的访问和存储的统一接口,dojo/store意图以一个简单.易于使用和扩展的API来,替代.集合和改善 dojo/data 和dojox/storage .基于HTM ...

  8. dojo 十 ajax dojo/_base/xhr

    官方教程:Ajax with DojoAjax功能:1.从服务器加载静态数据2.从web服务中访问xml或json类型的数据3.将表单(form)数据发送到服务器4.刷新页面内容....Ajax在RI ...

  9. 现代DOJO(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/modern_dojo/index.html 你可能已经不用doio一段时间了,或者你一直想保持 ...

随机推荐

  1. 通过编写串口助手工具学习MFC过程--(十一)弹出模态型对话框

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  2. LabWindows/CVI 下载

    LabWindows/CVI 是National Instruments 公司(美国国家仪器公司,简称NI 公司)推出的交互式C 语言开发平台.LabWindows/CVI 将功能强大.使用灵活的C ...

  3. 初学Git——命令总结

    首先,感谢廖雪峰老师制作的Git教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...

  4. 事件,IO,select

    事件驱动模型 对于普通编程来说,代码遵循线性流程:开始-->代码A-->代码B-->代码C-->...-->结束,编程者知道代码的运行顺序,由编程者控制 事件驱动模型,流 ...

  5. 分页控件SSTab

    一.分页控件SSTab概述1.作用:采用分页形式查询或编辑数据表中数据.2.添加到控件箱菜单命令:工程 | 部件,选择:Microsoft Tabbed Dialog Control 6.0 (SP6 ...

  6. 负载均衡(二)DNS负载均衡

    一.DNS原理及解析过程详解 相信大家在平时工作中都离不开DNS解析,DNS解析是互联网访问的第一步,无论是使用笔记本浏览器访问网络还是打开手机APP的时候,访问网络资源的第一步必然要经过DNS解析流 ...

  7. vue项目中 指令 v-html 中使用过滤器filters功能

    转载于简书 链接:http://www.jianshu.com/p/29b7eaabd1ba 问题 2.0 filters only work in mustache tags and v-bind. ...

  8. 深入理解MySql事务

    事务是MySQL等关系型数据库区别于NoSQL的重要方面,是保证数据一致性的重要手段.本文将首先介绍MySQL事务相关的基础概念,然后介绍事务的ACID特性,并分析其实现原理. MySQL博大精深,文 ...

  9. 【C】题解 (五校联考3day2)

    分析 这道题看上去很恶心,实际上只用记录四坨东西就能打DP了:y坐标最小的向上射的点.y坐标最大的向下射的点.y坐标最大和最小的向右射的点,转移显然.注意,如果该状态的值为零就可以略过,否则会超时. ...

  10. [洛谷P2567] SCOI2010 幸运数字

    问题描述 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是&quo ...