javascript - 将对象格式化为所有对象数组
问题描述
这个问题很简单,没有明确的答案。我有一个对象,我的目标是将每个值和键转换为对象并推送到数组,例如下面的说明。
{
title: "This is a Title",
name: "This is a name"
}
转变为。
[
{title: "This is a Title"},
{name: "This is a name"}
]
解决方案
用于Object.entries
将对象转换为数组,然后将数组映射为所需格式的对象数组:
const obj = {
title: "This is a Title",
name: "This is a name"
};
const arr = Object.entries(obj)
.map(([key, value]) => ({ [key]: value }));
console.log(arr);
推荐阅读
- c# - 在匿名类型数组中包含空值/默认值的更简洁方法?
- python - 尝试在 tkinter 画布中显示图像时出错
- google-cloud-platform - 创建 TPU 时出错:“不能低于现有策略版本”
- html - 为什么当我将鼠标悬停在图像 div 上时我的图像会抖动
- php - 如何在 wordpress 中获取行数?
- java - 我如何编写“If/else Statement”来为用户提供答案?
- php - 使用 php 和 mysql 计算总值而不是行
- google-sheets - 检查名称以在选项卡上导入值
- php - woocommerce 类别和子类别标题挂钩
- aws-lambda - 如何为无服务器 AWS 划分 YML 文件