javascript - 从数组中获取特定值并将这些值转换为另一个数组
问题描述
我有一个如下所示的数组
[{
"title": "Apple iPhone 7 Plus 32 GB",
"category": "phone",
"brand": "apple",
"condition": "Used",
"price": 800,
"id": 0,
"description": "Apple"
}, {
"title": "Apple Ipad Air 32 GB",
"category": "tablet",
"brand": "apple",
"condition": "new",
"price": 1000,
"id": 1,
"description": "Apple"
},{
"title": "Supreme Box Logo Hooded Sweatshirt Navy",
"category": "clothes",
"brand": "Supreme",
"condition": "new",
"price": 1000,
"id": 2,
"description": "Supreme Size M"
},]
如何从上面的数组中只获取类别的值并将它们转换为这样的数组?
[{"phone","clothes","tablet"}]
解决方案
只需映射您的数组并获取category
每个数组。
也[{"phone","clothes","tablet"}]
无效;一个对象是成对的键和值,所以我假设你的意思是["phone","clothes","tablet"]
const items = [{
"title": "Apple iPhone 7 Plus 32 GB",
"category": "phone",
"brand": "apple",
"condition": "Used",
"price": 800,
"id": 0,
"description": "Apple"
}, {
"title": "Apple Ipad Air 32 GB",
"category": "tablet",
"brand": "apple",
"condition": "new",
"price": 1000,
"id": 1,
"description": "Apple"
},{
"title": "Supreme Box Logo Hooded Sweatshirt Navy",
"category": "clothes",
"brand": "Supreme",
"condition": "new",
"price": 1000,
"id": 2,
"description": "Supreme Size M"
},];
const newItems = items.map(item => item.category);
console.log(newItems)
推荐阅读
- python - BigQuery 不接受来自 protobuf 的二进制数据
- swift - 删除的文件导致错误 - Swift
- javascript - React - 从外部网站重定向到反应应用程序
- java - 无法处理打印类路径
- python - 在 python 中生成清晰的巨型热图
- sql - 在 SQL Server 中提取 json 对象数据
- .htaccess - .htaccess 使用特殊 url 和 ID 数字作为参数重写
- node.js - GSuite 警报中心 API 返回“无法从请求或呼叫者身份推断客户 ID。” 错误
- linux - fuser 命令输出进程 ID 提取
- python - 在 LSTM 中包含分类特征和序列以进行序列预测的最佳实践?