logo
JR Academy · Blog职业洞察

如何js的object转化成数组, Converting an Object to an Array of Objects.

Javascript转化成数组 Javascript, Converting an Object to an Array of Objects.A while back I wrote an article that got some traction about converting an Array of Objects to an Object....

发布日期
阅读时长1 分钟
作者

关键词

Javascript转化, 数组 Javascript, Object , an Array of Objects.

浏览体验

高对比度 · 自适应布局

收录优化

结构化元数据 + 快速导航

如何转化你的Object 变成 array of objects。 有多种方式去实现,但是作为前端工程师更希望使用更加好看的方式去实现

You may be asking why, the answer is actually quite simple. In programing we are not always in control of the format in which our data is delivered to us. With this in mind it becomes important to understand how to convert our data into our desired format.

The Example

Using Javascript features there is an easy one liner that can achieve this result using Object.values().

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

For example :

const peopleObj = {
jim: {
name: "jim",
age: 20
},
tim: {
name: "tim",
age: 22
}
}
const peopleArray = Object.values(peopleObj)

In my opinion if you are using babel go ahead and use this, if not I will show below how you can do this with a more widely supported feature, Object.keys().

Note: The following example uses the peopleObj from the above example.

const peopleArray = Object.keys(peopleObj).map(i => peopleObj[i])

This second method is quite well supported. Check out both Object Values and Object Keys MDN documentation to learn where you may have issues with support and for more info on these features.

If you want to use Object.entries for getting only values you could use like this:

const object1 = { foo: 'bar', baz: 42 };

console.log(Object.entries(object1)[1]);

// expected output: Array ["baz", 42]

 

* The Object.entries() method returns an array of a given object's own enumerable property [key, value] pairs, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

作者Wang Yanbo
一键分享或复制链接
订阅更新

获取最新 AI 学习资源、技术教程和求职攻略,直接送达邮箱。

我们尊重您的隐私,不会发送垃圾邮件

近期开课hot

Vibe Coding提升班(for Tech)

start2026/01/10 00:00 (Sydney)

AI Engineer训练营04

start2026/01/11 00:00 (Sydney)

手撕全栈面试题班05期

start2026/01/19 00:00 (Sydney)

1v1免费职业咨询