Javascript Array Iteration

8 min read

Array iteration methods operate on every array item.

Багцын давталтын аргууд нь багцын бүх зүйл дээр ажилладаг.


Array.forEach() #

The forEach() method calls a function (a callback function) once for each array element.

forEach() арга нь функцийг (буцааж дуудах функц) багцын элемент тус бүрт нэг удаа дууддаг.

Example Жишээ #

var txt = ""; var numbers = [45, 4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value, index, array) { txt = txt + value + "<br>"; }

Note that the function takes 3 arguments:

Функц нь 3 аргумент шаарддаг болохыг анхаарна уу.

  • The item value

    Зүйлийн утга

  • The item index

    Барааны индекс

  • The array itself

    Багц өөрөө

The example above uses only the value parameter. The example can be rewritten to:

Дээрх жишээнд зөвхөн утга параметрийг ашигладаг. Жишээг дараахь байдлаар бичиж болно.

Example Жишээ #

var txt = ""; var numbers = [45, 4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value) { txt = txt + value + "<br>"; }

Array.forEach() is supported in all browsers except Internet Explorer 8 or earlier:

Array.forEach() нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбаруудаас бусад бүх хөтөч дээр дэмжигддэг:

Yes 9.0 Yes Yes Yes

Array.map() #

The map() method creates a new array by performing a function on each array element.

map() арга нь багцын элемент тус бүрт функцийг гүйцэтгэж шинэ багц үүсгэдэг.

The map() method does not execute the function for array elements without values.

map() арга нь багц элементүүдийн функцийг утгагүй гүйцэтгэдэггүй.

The map() method does not change the original array.

map()арга нь анхны багцыг өөрчлөхгүй.

This example multiplies each array value by 2:

Энэ жишээ нь багцын утга тус бүрийг 2-оор үржүүлнэ.

Example Жишээ #

var numbers1 = [45, 4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); function myFunction(value, index, array) { return value * 2; }

Note that the function takes 3 arguments:

Функц нь 3 аргумент шаарддаг болохыг анхаарна уу.

  • The item value

    Зүйлийн утга

  • The item index

    Барааны индекс

  • The array itself

    Багц өөрөө

When a callback function uses only the value parameter, the index and array parameters can be omitted:

Дахин дуудах функц нь зөвхөн утга параметрийг ашигладаг бол индекс ба багцын параметрүүдийг орхигдуулж болно.

Example Жишээ #

var numbers1 = [45, 4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); function myFunction(value) { return value * 2; }

Array.map() is supported in all browsers except Internet Explorer 8 or earlier.

Array.map() нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбараас бусад бүх хөтөч дээр дэмжигддэг.

Yes 9.0 Yes Yes Yes

 

Array.filter() #

The filter() method creates a new array with array elements that passes a test.

filter() арга нь тестийг давсан багцын элементүүдтэй шинэ багц үүсгэдэг.

This example creates a new array from elements with a value larger than 18:

Энэ жишээ нь 18-аас их утгатай элементүүдээс шинэ багц үүсгэдэг.

Example Жишээ #

var numbers = [45, 4, 9, 16, 25]; var over18 = numbers.filter(myFunction); function myFunction(value, index, array) { return value > 18; }

Note that the function takes 3 arguments:

Функц нь 3 аргумент шаарддаг болохыг анхаарна уу.

  • The item value
  • The item index
  • The array itself

In the example above, the callback function does not use the index and array parameters, so they can be omitted:

Дээрх жишээнд дуудлага хийх функц нь индекс ба багцын параметрүүдийг ашигладаггүй тул тэдгээрийг орхиж болно.

Example Жишээ #

var numbers = [45, 4, 9, 16, 25]; var over18 = numbers.filter(myFunction); function myFunction(value) { return value > 18; }

Array.filter() is supported in all browsers except Internet Explorer 8 or earlier.

Array.filter() нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбараас бусад бүх хөтөч дээр дэмжигддэг.

Yes 9.0 Yes Yes Yes

Array.reduce() #

The reduce() method runs a function on each array element to produce (reduce it to) a single value.

reduce() арга нь багцын элемент бүр дээр нэг утга үүсгэх (багасгах) функцийг ажиллуулдаг.

The reduce() method works from left-to-right in the array. See also reduceRight().

reduce() арга нь массивын зүүнээс баруун тийш ажилладаг. Мөн reduceRight() -ийг үзнэ үү.

The reduce() method does not reduce the original array.

reduce() арга нь анхны массивыг багасгахгүй.

This example finds the sum of all numbers in an array:

Энэ жишээ нь багц дахь бүх тооны нийлбэрийг олно.

Example Жишээ #

var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction); function myFunction(total, value, index, array) { return total + value; }

Note that the function takes 4 arguments:

Функц нь 4 аргумент шаарддаг болохыг анхаарна уу.

  • The total (the initial value / previously returned value)

    Нийт (анхны утга / өмнө буцаагдсан утга)

  • The item value
  • The item index
  • The array itself

The example above does not use the index and array parameters. It can be rewritten to:

Дээрх жишээнд индекс ба багцын параметрүүдийг ашигладаггүй. Үүнийг дараахь байдлаар бичиж болно.

Example Жишээ #

var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction); function myFunction(total, value) { return total + value; }

The reduce() method can accept an initial value:

reduce() арга нь эхний утгыг хүлээн авах боломжтой.

Example Жишээ #

var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction, 100); function myFunction(total, value) { return total + value; }

Array.reduce() is supported in all browsers except Internet Explorer 8 or earlier.

Array.reduce () нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбаруудаас бусад бүх хөтөч дээр дэмжигддэг.

Yes 9.0 Yes Yes Yes

Array.reduceRight() #

The reduceRight() method runs a function on each array element to produce (reduce it to) a single value.

reduceRight() арга нь багцын элемент бүр дээр нэг утга үүсгэх (багасгах) функцийг ажиллуулдаг.

The reduceRight() works from right-to-left in the array. See also reduce().

reduceRight() нь багцын баруун-зүүнээс ажилладаг. Мөн reduce() хэсгийг үзнэ үү.

The reduceRight() method does not reduce the original array.

reduceRight() арга нь анхны багцыг багасгахгүй.

This example finds the sum of all numbers in an array:

Энэ жишээ нь массив дахь бүх тооны нийлбэрийг олно.

Example Жишээ #

var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduceRight(myFunction); function myFunction(total, value, index, array) { return total + value; }

Note that the function takes 4 arguments:

Функц нь 4 аргумент шаарддаг болохыг анхаарна уу.

  • The total (the initial value / previously returned value)
  • The item value
  • The item index
  • The array itself

The example above does not use the index and array parameters. It can be rewritten to:

Дээрх жишээнд индекс ба массивын параметрүүдийг ашигладаггүй. Үүнийг дараахь байдлаар бичиж болно.

Example Жишээ #

var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduceRight(myFunction); function myFunction(total, value) { return total + value; }

Array.reduceRight() is supported in all browsers except Internet Explorer 8 or earlier.

Array.reduceRight () нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбараас бусад бүх хөтөч дээр дэмжигддэг.

Yes 9.0 Yes Yes Yes

Array.every() #

The every() method check if all array values pass a test.

Бүх () арга нь багцын бүх утга тестийг давсан эсэхийг шалгана.

This example check if all array values are larger than 18:

Энэ жишээнд бүх массивын утга 18-аас их байгаа эсэхийг шалгана уу:

Example Жишээ #

var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.every(myFunction); function myFunction(value, index, array) { return value > 18; }

Note that the function takes 3 arguments:

Функц нь 3 аргумент шаарддаг болохыг анхаарна уу.

  • The item value
  • The item index
  • The array itself

When a callback function uses the first parameter only (value), the other parameters can be omitted:

Дахин дуудлага хийх функц нь зөвхөн эхний параметрийг (утга) ашиглахад бусад параметрүүдийг орхигдуулж болно.

Example Жишээ #

var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.every(myFunction); function myFunction(value) { return value > 18; }

Array.every() is supported in all browsers except Internet Explorer 8 or earlier.

Array.every() нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбараас бусад бүх хөтөч дээр дэмжигддэг.

Yes 9.0 Yes Yes Yes

Array.some() #

The some() method check if some array values pass a test.

some() арга нь багцын зарим утга тестийг давсан эсэхийг шалгана.

This example check if some array values are larger than 18:

Энэ жишээ нь багцын зарим утга 18-аас их байгаа эсэхийг шалгана.

Example Жишээ #

var numbers = [45, 4, 9, 16, 25]; var someOver18 = numbers.some(myFunction); function myFunction(value, index, array) { return value > 18; }

Note that the function takes 3 arguments:

Функц нь 3 аргумент шаарддаг болохыг анхаарна уу.

  • The item value
  • The item index
  • The array itself

Array.some() is supported in all browsers except Internet Explorer 8 or earlier

Array.some () нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбаруудаас бусад бүх хөтөч дээр дэмжигддэг

Yes 9.0 Yes Yes Yes

Array.indexOf() #

The indexOf() method searches an array for an element value and returns its position.

indexOf()арга нь багцыг элементийн утгыг хайж, байрлалыг нь буцааж өгдөг.

Note: The first item has position 0, the second item has position 1, and so on.

Тэмдэглэл: Эхний зүйл 0 байрлалтай, хоёр дахь зүйл 1 байрлалтай гэх мэт.

Example Жишээ #

Search an array for the item “Apple”:

“Apple” зүйлийг багцаас хайх:

var fruits = ["Apple", "Orange", "Apple", "Mango"]; var a = fruits.indexOf("Apple");

Array.indexOf() is supported in all browsers except Internet Explorer 8 or earlier.

Array.indexOf() нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбараас бусад бүх хөтөч дээр дэмжигддэг.

Yes 9.0 Yes Yes Yes

Syntax #

array.indexOf(item, start)
item Required. The item to search for.

Шаардлагатай. Хайх зүйл.

start Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the end.

Нэмэлт. Хайлтыг хаанаас эхлэх вэ. Сөрөг утга нь өгөгдсөн байрлалаас эцэс хүртэл тоолж эхлэх ба эцэс хүртэл хайх болно.

Array.indexOf() returns -1 if the item is not found.

Array.indexOf() зүйл олдоогүй тохиолдолд -1 буцаана.

If the item is present more than once, it returns the position of the first occurrence.

Хэрэв тухайн зүйл нэгээс илүү удаа байвал анхны тохиолдлын байрлалыг буцааж өгнө.


Array.lastIndexOf() #

Array.lastIndexOf() is the same as Array.indexOf(), but returns the position of the last occurrence of the specified element.

Array.lastIndexOf() нь Array.indexOf() -тай ижил боловч заасан элементийн сүүлчийн илрэлийн байрлалыг буцаана.

Example Жишээ #

Search an array for the item “Apple”:

“Apple” зүйлийг багцаас хайх:

var fruits = ["Apple", "Orange", "Apple", "Mango"]; var a = fruits.lastIndexOf("Apple");

Array.lastIndexOf() is supported in all browsers except Internet Explorer 8 or earlier.

Array.lastIndexOf() нь Internet Explorer 8 эсвэл түүнээс өмнөх хувилбараас бусад бүх хөтөч дээр дэмжигддэг.

Yes 9.0 Yes Yes Yes

Syntax #

array.lastIndexOf(item, start)
item Required. The item to search for
start Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the beginning

Array.find() #

The find() method returns the value of the first array element that passes a test function.

Find () арга нь туршилтын функцийг дамжуулсан багцын эхний элементийн утгыг буцаана.

This example finds (returns the value of) the first element that is larger than 18:

Энэ жишээ нь 18-аас их эхний элементийг олно (утгыг буцаана):

Example Жишээ #

var numbers = [4, 9, 16, 25, 29]; var first = numbers.find(myFunction); function myFunction(value, index, array) { return value > 18; }

Note that the function takes 3 arguments:

Функц нь 3 аргумент шаарддаг болохыг анхаарна уу.

  • The item value
  • The item index
  • The array itself

Array.find() is not supported in older browsers. The first browser versions with full support is listed below.

Array.find() -ийг хуучин хөтөч дээр дэмждэггүй. Бүрэн хөтөчтэй хөтөчийн анхны хувилбаруудыг дор жагсаав.

45 12 25 8 32

Array.findIndex() #

The findIndex() method returns the index of the first array element that passes a test function.

findIndex() арга нь туршилтын функцийг дамжуулсан багцын эхний элементийн индексийг буцаана.

This example finds the index of the first element that is larger than 18:

Энэ жишээнд эхний элементийн индексийг 18-аас их хэмжээгээр олсон болно.

Example Жишээ #

var numbers = [4, 9, 16, 25, 29]; var first = numbers.findIndex(myFunction); function myFunction(value, index, array) { return value > 18; }

Note that the function takes 3 arguments:

Функц нь 3 аргумент шаарддаг болохыг анхаарна уу.

  • The item value
  • The item index
  • The array itself

Array.findIndex() is not supported in older browsers. The first browser versions with full support is listed below.

Array.findIndex() -ийг хуучин хөтөч дээр дэмждэггүй. Бүрэн хөтөчтэй хөтөчийн анхны хувилбаруудыг дор жагсаав.

45 12 25 8 32

Powered by BetterDocs

Leave a Reply