Home >Web Front-end >JS Tutorial >How to convert object to array in javascript

How to convert object to array in javascript

青灯夜游
青灯夜游Original
2021-06-17 17:34:0526625browse

Method: 1. Use the "Array.from(object)" statement; 2. Use the "Object.values(object)" statement; 3. Use the "Object.keys(object)" statement; 4. Use the "Object.entries(object)" statement.

How to convert object to array in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript object to array

1. Array.from() method , used for shallow copy of array. It is to convert an array-like object or traversable object into a real array.

eg:

 let obj = {
        0: ‘nihao‘,
        1: ‘haha‘,
        2: ‘gansha‘,
        ‘length‘: 3
    } 
    let arr = Array.from(obj)
    console.log(arr);

How to convert object to array in javascript

tips: 1. The object must have a length attribute, and the returned array length depends on the length length
2.key value Must be a numerical value

2. Object.values(object)

#tips: The difference from the first one is that the length attribute is not required and all enumerable objects of an object are returned. Attribute value

Here is a brief talk about enumerability. At present, I haven't understood the use of this enumerability. It probably means that each attribute of the

object has a description object, which is used to describe and control the behavior of the attribute. Use Object. getOwnPropertyDescriptor method to get the description object (set with Object.defineProperty method).

I will write about this enumerability after careful study in the future!

eg:

let obj = {
        0: ‘nihao‘,
        1: ‘haha‘,
        2: ‘gansha‘,
    }
    let arr = Object.values(obj)
    console.log(arr);

How to convert object to array in javascript

3. Object.keys(object)

tips: Return an object An array composed of its own enumerable attributes. The order of the attribute names in the array is consistent with the order returned when using the for...in loop to traverse the object.

let obj = {
        0: ‘nihao‘,
        1: ‘haha‘,
        2: ‘gansha‘,
    }
    let arr = Object.keys(obj)
    console.log(arr);

How to convert object to array in javascript

4 , Object.entries(object)

tips: Returns an array of key-value pairs of the enumerable properties of the given object itself

let obj = {
        0: ‘nihao‘,
        1: ‘haha‘,
        2: ‘gansha‘,
    }
    let arr = Object.entries(obj)
    console.log(arr);

How to convert object to array in javascript

【 Related recommendations: javascript learning tutorial

The above is the detailed content of How to convert object to array in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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