Dojo/_ base/Array

Source: Internet
Author: User

Official Address: http://dojotoolkit.org/reference-guide/1.10/dojo/_base/array.html#dojo-base-array

The array module dojo is well encapsulated. If you want to call it, you must first load the module:

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

 

Indexof ()

Returned value: the position where the string appears for the first time in the array. If no value is found,-1 is returned by default.

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

 

Example:

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 ()

Returned value: the last position of the string in the array. If no value is found,-1 is returned by default.

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

Example:

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 ()

Traverse the array or nodelists.

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

The third parameter adjusts the access range of foreach.

Example:

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);});

If you want to break this loop, you can use 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 ()

Returned value: the result array after array filtering.

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

Example:

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 ()

Returned value: Another modified array.

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

Example:

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>

Sometimes in order not to modify the original array itself, Dojo provides the dojo/_ base/lang: Clone ()

Example:

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 ()

Return Value: true or false. If one of the methods is true, true is returned. It is often used in if statements.

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

Example: Check whether there is a value greater than or equal to 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 ()

Return Value: true or false. True is returned only when each item in the method returns true. It is often used in if statements.

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

Example: Check whether each item is greater than 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>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.