[英]Map json data from array with several properties
我有以下 json object
https://codebeautify.org/jsonviewer/cb01bb4d
obj =
{
"api": "1.0.0",
"info": {
"title": "Events",
"version": "v1",
"description": "Set of events"
},
"topics": {
"cust.created.v1": { //Take this value
"subscribe": {
"summary": "Customer Register Event v2", //Take this value
"payload": {
"type": "object",
"required": [
"storeUid"
],
"properties": {
"customerUid": {
"type": "string",
"description": "Email of a Customer",
"title": "Customer uid"
}
}
}
}
},
"qu.orderplaced.v1": { //Take this value
"subscribe": {
"summary": "Order Placed", //Take this value
"payload": {
"type": "object",
"required": [
"quoteCode"
],
"properties": {
"quoteCode": {
"type": "string",
"example": "762",
"title": "Quote Code"
}
}
}
}
}
}
}
我需要 map 從 json 對象到 javascript 數組的值
例如:
MyArray = [
{
Label: “cust.created.v1”,
Description: "Customer Register Event v2"
},
{
Label: “qu.orderplaced.v1”,
Description: "Order Placed",
}
]
我需要 map 這兩個值,每個實例的鍵 => label (例如“cust.created.v1”)和摘要 => 每個實例的描述
我試過用map 來做,但我很難用鑰匙和里面的屬性來做,是否可以用 map 來做?
獲取 object 然后 map 的entries
:
var obj = { "api": "1.0.0", "info": { "title": "Events", "version": "v1", "description": "Set of events" }, "topics": { "cust.created.v1": { "subscribe": { "summary": "Customer Register Event v2", "payload": { "type": "object", "required": [ "storeUid" ], "properties": { "customerUid": { "type": "string", "description": "Email of a Customer", "title": "Customer uid" } } } } }, "qu.orderplaced.v1": { "subscribe": { "summary": "Order Placed", "payload": { "type": "object", "required": [ "quoteCode" ], "properties": { "quoteCode": { "type": "string", "example": "762", "title": "Quote Code" } } } } }}} var result = Object.entries(obj.topics).map(([k,v])=>({Label:k, Description:v.subscribe.summary})); console.log(result);
Object.entries 是您問題的關鍵;)
Object.entries(obj.topics).map(topic => ({
Label: topic[0],
Description: topic[1].subscribe.summary
}))
如果您不確定是否為 topic[1].subscribe,您可以使用topic[1].subscribe?.summary
或topic[1].subscribe && topic[1].subscribe.summary
也可以解構 Object.entries 的內部 arrays,可能會更干凈一些
const obj = { "api": "1.0.0", "info": { "title": "Events", "version": "v1", "description": "Set of events" }, "topics": { "cust.created.v1": { "subscribe": { "summary": "Customer Register Event v2", "payload": { "type": "object", "required": [ "storeUid" ], "properties": { "customerUid": { "type": "string", "description": "Email of a Customer", "title": "Customer uid" } } } } }, "qu.orderplaced.v1": { "subscribe": { "summary": "Order Placed", "payload": { "type": "object", "required": [ "quoteCode" ], "properties": { "quoteCode": { "type": "string", "example": "762", "title": "Quote Code" } } } } }}} const arr = Object.entries(obj.topics).map(([Label, content]) => ({ Label, Description: content.subscribe?.summary })) console.log(arr)
您可以對主題中的所有鍵進行 map,對於這些項目中的每一項,您都可以從 object 中提取任何數據,如下所示:
Object.keys(obj.topics).map((key) => { return {Label: key, Description: obj.topics[key].subscribe.summary} })
你可以這樣做
let obj = { "api": "1.0.0", "info": { "title": "Events", "version": "v1", "description": "Set of events" }, "topics": { "cust.created.v1": { "subscribe": { "summary": "Customer Register Event v2", "payload": { "type": "object", "required": [ "storeUid" ], "properties": { "customerUid": { "type": "string", "description": "Email of a Customer", "title": "Customer uid" } } } } }, "qu.orderplaced.v1": { "subscribe": { "summary": "Order Placed", "payload": { "type": "object", "required": [ "quoteCode" ], "properties": { "quoteCode": { "type": "string", "example": "762", "title": "Quote Code" } } } } }}} let outputArray = Object.keys(obj.topics).map((key)=>( { Label: key, Description: obj.topics[key]['subscribe']['summary'] } )) console.log(outputArray)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.