Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use entries () in js

Shulou Source: shulou.com Published: 2022-06-02 20:57:55 09月11日 Update

This article mainly shows you "how to use entries () in js". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use entries () in js".

1. Detailed explanation of the syntax of the entires () method

The entries () method returns an iterative object of an array that contains the key-value pair (key/value) of the array.

The index value of the array in the iterative object is as key, and the array element is as value. Its prototype (_ _ proto__:Array Iterator) has a next method that can be used to iterate through the [key,value] of the original array. You need to know something about iterators here.

2. The entires () method is commonly used and notes 2.1. returns the 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 for...of... Const options = [1,5]; for (const [index, value] of options.entries ()) {console.log (value) } / / 01 / / 1 undefined / / 2 undefined / / 3 undefined / / 452.3 two-dimensional array row sort 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, the sortTwo method first obtains the iterative object of the input array, and then defines an initialization identified as true, and then recursively calls the next method of the iterative object entires to assign a value to the res object to determine the d one attribute of the res object. If the value is true, it can be recursive, and res.value corresponds to each row of the two-dimensional array. You can sort the item. If the value is flase, you can end the recursion.

The above is all the contents of the article "how to use entries () in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Objects arrays iterations methods content articles recursion knowledge two-dimensional learning help sorting one line code elements prototypes attributes common easy to understand more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information Microsoft Linux OPPO Reno