Home > Web Front-end > JS Tutorial > body text

Summary of JS methods to convert non-array objects to arrays

亚连
Release: 2018-05-28 17:42:48
Original
1642 people have browsed it

This article mainly introduces you to some methods of converting JS from non-array objects to arrays, namely Array.prototype.slice.call(obj), Array.from(obj), [...obj] and Object. For detailed implementation methods of values(obj) and other methods, friends in need can refer to it.

Preface

This article mainly summarizes some methods of converting JS from non-array objects to arrays, and shares them for your reference and study. The following is not Enough said, let’s take a look at the detailed introduction.

Array.prototype.slice.call(obj)

This method can convert an array-like object into an array, the so-called array-like object, It is an object containing length and index properties.

The length of the returned array depends on the value of the length property of the object, and the value of the non-index property, or the value with an index greater than length will not be returned to the array.

The actual hammer is as follows

let obj = {
 '0': 3,
 '1': 13,
 '2': 23,
 '3': 33,
 'length': 3,
 'name': 330
}
let arr = Array.prototype.slice.call(obj)
// [3, 13, 23]
Copy after login

Concise writing method[].slice.call(obj)

Array.from(obj)

This method can convert array-like objects and iterable objects into arrays

Array-like objects have been mentioned above. What are iterable objects?

  • Array, Set, Map and String are all iterable objects (WeakMap/WeakSet are not iterable objects)

  • String changes Became an iterable object, solving the coding problem

  • These objects have default iterators, that is, they have the Symbol.iterator attribute

  • You can use the for of loop

  • All iterators created by generators are iterable objects

  • document.getElementsByTagName(" p") Returns an iterable object but not an array
    Array.isArray(document.getElementsByTagName('p')) Returns false

Create iterable objects through generators

let obj = {
 '0': 3,
 '1': 13,
 '2': 23,
 '3': 33
}

function *createIterator(obj){
 for(let value in obj){
  yield obj[value]
 }
}

let iterator = createIterator(obj)
let arr = Array.from(iterator)
// [3, 13, 23, 33]
Copy after login

Transform the object itself to make it an iterable object

By default, the objects defined by the developer are all non-iterable objects, but if Symbol.iterator If you add a generator to the attribute, you can turn it into an iterable object

let obj = {
 '0': 3,
 '1': 13,
 '2': 23,
 '3': 33
}

obj[Symbol.iterator] = function* () {
 for(let value in this){
  yield this[value]
 }
}

let arr = Array.from(obj)
// [3, 13, 23, 33]
Copy after login

Method to determine whether the object is an iterable object

typeof obj[Symbol.iterator] === 'function'
Copy after login

A little extension of for of and forEach and for in

for of is used to loop iterable objects, including Array, Set, Map, and strings

And Array, Set, and Map all have forEach methods

In addition, NodeList is not an Array, Set, or Map, but an iterable object that can be traversed using for of.

In addition, when using for of to loop objects, it can be terminated early through break, but forEach cannot jump out of the loop in advance.

for in Traverse the enumerable properties of the object, including properties on its prototype chain, and the order is not guaranteed

To traverse the enumerable properties of the object itself, use hasOwnProperty () Method to determine whether the properties are the properties of the object itself

Object.getOwnPropertyNames(obj) , return the enumerable or non-enumerable properties of the object itself

Anyway, we’ve gone too far, so let’s go a little further, Object.assign() The method copies the values ​​of all enumerable properties from one or more source objects to the target object

[…obj]

The spread operator can convert an iterable object into an array

For example, [...'obj '] Return["o", "b", "j"]

String deduplication

[...new Set ('objobj')]

Object.values(obj)

By default, the objects defined by the developer are It is not an iterable object, but provides methods to return iterators

  • entries()

  • values()

  • keys()

By using these methods, you can return the related array

Unlike array-like objects that require objects to have length values, Object .values(obj) Returns a collection of enumerable attribute values ​​​​of the object itself

let obj = {
 '0': 3,
 '1': 13,
 '2': 23,
 '3': 33
}
let arr = Object.values(obj) // [3, 13, 23, 33]
Copy after login

The relationship between strings and arrays

To a large extent, you can Treat strings as string arrays,

all have length attributes,

all have concat() / indexOf() / includes() / slice() Method

However, it is worth noting that there is no method on string that can modify its own content in place, all of which return a new string

string There is also a repeat() method to create a specified number of string copies

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Preliminary understanding of JavaScript, Ajax, jQuery, and comparison of the relationship between the three

Jquery and PHP combined to achieve AJAX long-term Polling

Jquery specific examples introduce when to use AJAX and where AJAX should be used

The above is the detailed content of Summary of JS methods to convert non-array objects to arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template