Home > Web Front-end > Front-end Q&A > How to convert object into array javascript

How to convert object into array javascript

PHPz
Release: 2023-05-16 09:13:07
Original
6142 people have browsed it

In JavaScript, objects (Object) and arrays (Array) are commonly used data types, and it is often necessary to convert objects into arrays. Below we will introduce how to convert objects into arrays.

Method 1: Use Object.keys()

The Object.keys() method is used to obtain all property names of the object and return an array containing these property names. Therefore, we can use the Object.keys() method to store all the property names of the object into an array, and then get an array with all the property names of the object.

For example, we can convert the following objects into arrays:

let obj = {'name': 'Tom', 'age': 20, 'sex': 'male'};
let arr = Object.keys(obj);
console.log(arr);  // 输出 ['name', 'age', 'sex']
Copy after login

Method 2: Use Object.values()

Object.values() method and Object.keys The () method is similar, except that it returns an array of attribute values ​​of the object instead of an array of attribute names. Therefore, we can also store all the attribute values ​​of the object into an array through the Object.values() method, and then get an array with all the attribute values ​​of the object.

For example, we can convert the following objects into arrays:

let obj = {'name': 'Tom', 'age': 20, 'sex': 'male'};
let arr = Object.values(obj);
console.log(arr);  // 输出 ['Tom', 20, 'male']
Copy after login

Method 3: Use the Object.entries()

Object.entries() method to convert all of the objects The attribute name and attribute value are combined into a two-dimensional array. The first item in the array is the attribute name, and the second item is the attribute value. Therefore, we can also use the Object.entries() method to convert objects into arrays.

For example, we can convert the following objects into arrays:

let obj = {'name': 'Tom', 'age': 20, 'sex': 'male'};
let arr = Object.entries(obj);
console.log(arr);  // 输出 [['name', 'Tom'], ['age', 20], ['sex', 'male']]
Copy after login

Generally, we use the Object.keys() or Object.values() method to meet our daily needs. The Object.entries() method is usually used to handle two-dimensional arrays.

Summary:

The above three conversion methods each have their own advantages and disadvantages. Which one to use needs to be chosen according to the specific scenario. No matter which method is used, the array obtained after conversion retains the original attribute name or attribute value of the object, so data processing can be more convenient.

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

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