js array entries() Get iteration method

js array entries() Get iteration method

1. Detailed syntax of entires() method

entries() method returns an array iterator object that contains the array's (key/value) .

The index value of the array in the iteration object is used as key and the array element is used as value . Its prototype (__proto__:Array Iterator) has a next method that can be used to traverse the iterator to obtain [key,value] of the original array. Here you need to know the relevant iterator knowledge.

2. Common uses and precautions of the entires() method

2.1 Returning an iterator object

    var arr = ["red", "blue", "green"]
    var x = arr.entries()
    console.log(x); // Array Iterator {}
    console.log(x.next()) //{value: Array:[0, "red"],done:false}
    console.log(x.next()) //{value: Array:[1, "blue"],done:false}
    console.log(x.next()) //{value: Array:[2, "green"],done:false}
    console.log(x.next()) //{value: undefined, done: true}

2.2 Use of for...of...

    const options = [1, , , , 5];
    for (const [index, value] of options.entries()) {
      console.log(value);
    }
    // 0 1
    // 1 undefined
    // 2 undefined
    // 3 undefined
    // 4 5

2.3 Sorting rows of a two-dimensional array

    function sortTwo(arr) {
      var entries = arr.entries()
      var flag = true
      while (flag) {
        var res = entries.next()
        if (!res.done) {
          res.value[1].sort((a, b) => a - b);
          flag = true
        } else {
          flag = false
        }
      }
      return arr
    }
    var arr = [[1, 3, 2], [44, 33], [11, 55, 44, 33]]
    sortTwo(arr)
    console.log(arr); // [[1, 2, 3], [33, 44], [11, 33, 44, 55]]

In the above code, sortTwo method first obtains the iteration object of the passed array, and then defines an initialization flag of true , recursively calls the next method of the iteration object entires to assign it to res object, and judges the d one attribute of res object. If the value is true , it means recursion is possible. res.value corresponds to each row of the two-dimensional array, and the item can be sorted. If the value is flase , it means the recursion is ended.

Summarize:

This is the end of this article about js array entries() to get iteration method. For more related js array entries() to get iteration content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of common methods of JavaScript arrays
  • Summary of several common methods of JavaScript arrays
  • Summary of some common methods of JavaScript array
  • JavaScript Interview: How to implement array flattening method
  • A brief discussion on the built-in traversal methods of JS arrays and their differences
  • Detailed explanation of JS array methods

<<:  Docker container accesses the host's MySQL operation

>>:  Web page custom selection box Select

Recommend

How to disable IE10's password clear text display and quick clear function

IE10 provides a quick clear button (X icon) and a ...

Detailed explanation of replace into example in mysql

Detailed explanation of replace into example in m...

MySQL data type selection principles

Table of contents Small but beautiful Keep it sim...

In-depth analysis of the Tomcat server of Centos 7 system

Table of contents 1. The origin of tomcat 1. Tomc...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

Website Color Schemes Choosing the Right Colors for Your Website

Does color influence website visitors? A few year...

The complete version of the common Linux tool vi/vim

Why learn vim Linux has a large number of configu...

CentOS 7 switching boot kernel and switching boot mode explanation

centos7 switch boot kernel Note: If necessary, it...

Solution to the problem of installing MySQL compressed version zip

There was a problem when installing the compresse...

Vue implements carousel animation

This article example shares the specific code of ...

html base url tag

Its function is to set a global style. Then your s...

JavaScript to achieve uniform animation effect

This article example shares the specific code for...

Tutorial on installing mysql5.7.17 via yum on redhat7

The RHEL/CentOS series of Linux operating systems...

The meaning of status code in HTTP protocol

A status code that indicates a provisional respon...