[英]How do I convert JSON values into a key value pair pir
I have a JSON in the following format and need to convert the 2 values into a Key / Value pair in javascript我有一个 JSON 格式如下,需要将 2 个值转换为 javascript 中的键/值对
"column_values": [ { "id": "status", "text": "Working on it" } ] “column_values”:[{“id”:“状态”,“文本”:“正在处理”}]
I need the result to be我需要结果是
"column_values"[{"status": "Working on it"}] "column_values"[{"status": "正在处理中"}]
I need the code to iterate through the column_values array and convert all the sets of id and text pairs to the key = id value: Value = text:values我需要代码来遍历 column_values 数组并将所有 id 和 text 对集合转换为 key = id value: Value = text:values
Is my result possible?我的结果可能吗?
Additional Information...附加信息...
I am parsing a response from monday.com api in zapier.我正在解析来自 zapier 中 monday.com api 的响应。
the api payload is contained in api 有效载荷包含在
const results = response.json;常量结果=响应。json;
the full api payload is完整的 api 有效载荷是
{
"data": {
"boards": [
{
"name": "Test Board",
"items": [
{
"name": "Name Change",
"id": "625495642",
"column_values": [
{
"id": "person",
"text": ""
},
{
"id": "subitems",
"text": "Subitem 1, Subitem 2"
},
{
"id": "status",
"text": "Working on it"
},
{
"id": "dropdown",
"text": "Test1"
},
{
"id": "formula",
"text": ""
}
]
}
]
}
]
},
"account_id": 1111
}
I need to the the code to parse the data and replace the column_values with the format above, and then pass the reformated payload to我需要代码来解析数据并将 column_values 替换为上述格式,然后将重新格式化的有效负载传递给
return results;返回结果;
You just Map the Array you start out with to an Array with the values.您只需将您开始使用的数组 Map 转换为具有值的数组。
var column_values = [ { "id": "status", "text": "Working on it" } ] var KeyValuePairs = column_values.map(cv => [cv.id,cv.text]); console.log(KeyValuePairs);
If every object is going to contain the id
and text
keys only, you can map it and delete the other keys.如果每个 object 只包含
id
和text
键,您可以 map 并删除其他键。
column_values = column_values.map(item => {
item[item.id] = item.text;
delete item.id;
delete item.text;
return item;
});
try this尝试这个
var column_values = [ { "id": "status", "text": "Working on it" } ] var res = column_values.map(x => ({[x.id]: x.text})) console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.