Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use entries () in js

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report