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

Reasons and solutions for not being able to detect array changes in Vue2

Table of contents Workaround Why can't I moni...

Summary of several principles that should be followed in HTML page output

1. DOCTYPE is indispensable. The browser determin...

Detailed examples of how to use the box-shadow property in CSS3

There are many attributes in CSS. Some attributes...

How to understand JS function anti-shake and function throttling

Table of contents Overview 1. Function debounce 2...

Solve the problem that await does not work in forEach

1. Introduction A few days ago, I encountered a p...

Vue opens a new window and implements a graphic example of parameter transfer

The function I want to achieve is to open a new w...

Tomcat's class loading mechanism process and source code analysis

Table of contents Preface 1. Tomcat class loader ...

Detailed tutorial on installation and configuration of nginx under Centos7

Note: The basic directory path for software insta...

You may need a large-screen digital scrolling effect like this

The large-screen digital scrolling effect comes f...

Detailed steps for installing and debugging MySQL database on CentOS7 [Example]

This example requires downloading and installing ...

Solution to 2059 error when connecting Navicat to MySQL

Recently, when I was learning Django, I needed to...