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

How to convert non-array objects into arrays

php中世界最好的语言
Release: 2018-05-08 15:37:05
Original
1953 people have browsed it

This time I will show you how to convert a non-array object into an array, and what are the precautions for converting a non-array object into an array. The following is a practical case, let's take a look.

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 through generators are iterable objects

  • document. getElementsByTagName<a href="http://www.php.cn/code/8145.html" target="_blank">("p") </a>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 not Iterate objects, but if you add a generator to the

Symbol.iterator property, 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 an 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 are all There is a forEach method

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 an object, it can be terminated early through break. And forEach cannot break 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 object Its own enumerable properties, use the

hasOwnProperty() method to determine whether the property is the object's own property

Object.getOwnPropertyNames(obj) , return the object itself to be enumerable Enumerable or non-enumerable attributes

Anyway, we have gone too far, so let’s go a little further.

Object.assign() The method changes the values ​​​​of all enumerable attributes from one or more Source object copies to target object

[…obj]

The spread operator can convert an iterable object into Array

For example,

[...'obj'] Returns ["o", "b", "j"]

characters String deduplication

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

##Object.values(obj)By default, the objects defined by the developer are all non-iterable objects, but methods are provided to return iterators

    entries()
  • values()
  • ##keys()
  • By using these methods, you can return the related array
  • 与类数组对象需要对象有 length 值不同,Object.values(obj) 返回对象自身可枚举属性值的集合

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

字符串与数组的关系

在很大程度上,可以将字符串看成字符串数组,

都有 length 属性

都有 concat() / indexOf() / includes() / slice() 方法

不过值得注意的是, string 上没有方法可以原地修改它自身的内容,都是返回新的 string

string 还有个 repeat() 方法,创建指定数量的字符串副本

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

node自定义命令行工具

Vue全局引入bass.scss步骤详解

The above is the detailed content of How to convert non-array objects into 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!