javascript - 从数组中获取特定值并将值转换为另一个数组

标签 javascript arrays json

我有一个如下所示的数组

[{
"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"}]

最佳答案

只需映射数组并获取每个数组的类别

此外,[{"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)

关于javascript - 从数组中获取特定值并将值转换为另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60330264/

相关文章:

javascript - AngularJS with Rails 无法识别 $scope

c++ - C & C++ 中数组的动态内存分配

.net - 确保 .NET 中的 json 键是小写的

javascript - 如何在 Chart.js 中同时显示堆叠和非堆叠条形图

c# - 如何在 C# Asp.net 中注销 Page.ClientScript

c - 传递给另一个方法的结构体数组

mysql - 查询mysql中某个json类型的问题

javascript - 比较 JSON 返回的参数值

javascript - EJS Javascript 中分解语句的规则是什么?

java - BufferedImage.getRGB(int, int, int, int, int[], int, int) 如何工作?