如何使用 JavaScript 将对象对象转换为对象数组对象?
想要将对象对象转换为对象数组对象,使用 Object.fromEntries() 与 map() 的概念即可实现。
示例
const studentDetails = {
'details1': {Name: "John", CountryName: "US"},
'details2': {Name: "David", CountryName: "AUS"},
'details3': {Name: "Bob", CountryName: "UK"},
};
console.log(
Object.fromEntries(Object.entries(studentDetails).map(([key,
value]) => [key, [value]]))
);想要运行以上程序,你需要使用以下命令 −
node fileName.js.
这里,我的文件名是 demo45.js。
输出
这会产生以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo45.js
{
details1: [ { Name: 'John', CountryName: 'US' } ],
details2: [ { Name: 'David', CountryName: 'AUS' } ],
details3: [ { Name: 'Bob', CountryName: 'UK' } ]
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP