gpt4 book ai didi

javascript - 在 Javascript 对象中查找和替换键

转载 作者:行者123 更新时间:2023-11-30 11:12:41 25 4
gpt4 key购买 nike

我正在访问返回的 JSON,如下所示:

[
{
"itemType": "SelectionTitle",
"name": "1105F.MID",
"active": true,
"isFactoryDefault": false,
"factoryCode": "",
"seasons": [],
"denominations": [],
"groups": [],
"length": 0,
"_id": "5ada2217c114ca048e1db9b0",
"created_by": "5ab57289d8d00507b29a3fdd",
"selectionFile": {
"itemType": "SelectionFile",
"name": "1105F.MID",
"active": true,
"isFactoryDefault": false,
"selectionType": "Music",
"sfzFile": "",
"destination": "/data/uploads",
"encoding": "7bit",
"fieldname": "file",
"filename": "782f49a7cd72b865b4e2d286816792e7",

...
"found": true,
"flError": false,
"error_strings": [],
"_id": "5ada2217c114ca048e1db9af",
"created_by": "5ab57289d8d00507b29a3fdd",
"slug": "1105fmid",
"__v": 0,
"createdAt": "2018-04-20T17:23:35.216Z",
"updatedAt": "2018-04-20T17:23:35.788Z",
"selectionTitles": null,
"id": "5ada2217c114ca048e1db9af"
},
"slug": "1105fmid",
"createdAt": "2018-04-20T17:23:35.285Z",
"updatedAt": "2018-04-20T17:23:35.285Z",
"__v": 0,
"id": "5ada2217c114ca048e1db9b0"
}, ...

我正在使用的 react-select 节点模块采用键“标签”来生成填充的下拉列表。

JSON 来自网络,因此我无法控制 JSON 的设置方式。如何解析 JSON 以找到“name”的所有实例并将该键替换为“label”?

例如 "name": 1105F.MID"应该改为 "label": "1105.MID"

将整个东西转换为字符串并使用 javascript 方法查找/替换会不会效率低下?

最佳答案

假设您的 JSON 数组存储在变量 data 中:

data.forEach(item => item.label = item.name)

这足以将 name 属性复制为数组中每个项目的 label 属性。

关于javascript - 在 Javascript 对象中查找和替换键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53014532/

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