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

问题描述 投票:0回答:1

我有一个如下数组

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

如何从上面的数组中仅获取category的值并将其转换为数组?

[{"phone","clothes","tablet"}]
javascript arrays json
1个回答
1
投票

仅通过数组映射并获取每个数组的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)
© www.soinside.com 2019 - 2024. All rights reserved.