gpt4 book ai didi

javascript - 如何保留 json 数组中的特定元素并删除其他元素?

转载 作者:行者123 更新时间:2023-11-28 11:31:46 25 4
gpt4 key购买 nike

我有以下 json 数组:

[
{ "id": "1", "title": "Pharmacy", "desc": "xyz"},
{ "id": "21", "title": "Engineering", "desc": "xyz"},
{ "id": "30", "title": "Agriculture", "desc": "xyz"},
...
]

我只想保留标题元素并删除其他元素。我尝试使用 php 和 javascript 解决这个问题。

最佳答案

在 JavaScript 中,使用 Array.prototype.map() :

let array = [
{ "id": "1", "title": "Pharmacy", "desc": "xyz"},
{ "id": "21", "title": "Engineering", "desc": "xyz"},
{ "id": "30", "title": "Agriculture", "desc": "xyz"}
];
let mapped = array.map(i => ({title: i.title}));

console.log(mapped);

关于javascript - 如何保留 json 数组中的特定元素并删除其他元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42359516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com