javascript - 从对象获取属性以转换为简单数组 - javascript
问题描述
我有这个数组:
[
{
"id": "5e235b42a21b160a40c4a82f",
"title": "category one"
},
{
"id": "5e235b3ea21b160a40c4a82e",
"title": "category two"
}
]
我想将其转换为以下数组:
[
"5e235b42a21b160a40c4a82f",
"5e235b3ea21b160a40c4a82e"
]
有人有一个简单的建议吗?
解决方案
The best way to do this is by iterating over the Object values and taking each id
value.
The simplest way of doing this is by using .map()
like so:
const newArray = myArray.map(item => item.id);
This should take each item and form the id
property into an array.
推荐阅读
- python - NetCDF4 Python 无法识别属性
- node.js - 使用 Mongoose Node JS 将字节数组转换为十六进制字符串
- html - 将 h4 标签与覆盖颜色上的按钮对齐
- java - 如何根据通过 Selenium 和 Java 提供的 HTML 单击文本为 Continue 的按钮?
- swift - 如何快速计算一个恭维的十进制数?
- android - Android Room 使用更新但未在实体中设置所有列
- python - UserError 在值 0 上触发
- python - 为什么按下 PyQt 按钮时 matplotlib 的计时器会加快?
- java - 空对象引用 JSONArray 和 JSONObject
- sorting - ExtJS 4.2.2:如何在组合框顶部显示所选项目?