从具有多个属性的数组映射 json 数据
Posted
技术标签:
【中文标题】从具有多个属性的数组映射 json 数据【英文标题】:Map json data from array with several properties 【发布时间】:2020-10-05 20:18:32 【问题描述】:我有以下 json 对象
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"
我需要将 json 对象的值映射到 javascript 数组
例如:
MyArray = [
Label: “cust.created.v1”,
Description: "Customer Register Event v2"
,
Label: “qu.orderplaced.v1”,
Description: "Order Placed",
]
我需要映射两个值,每个实例的 key => label(例如“cust.created.v1”)和 summary => Description 来自每个实例
我尝试用map 来做,但我很难用 key 和里面的属性来做,可以用 map 做吗?
【问题讨论】:
【参考方案1】:获取对象的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);
【讨论】:
谢谢,我在 TS 中运行它,我收到以下错误:Object is of type 'unknown'.ts(2571)
对于这个 v.subscribe.summary
,知道如何克服这个问题吗?
@BenoOdr 将 map(([k,v] as any)
替换为 map(([k,v])
。它应该可以工作
我像var result = Object.entries(obj.topics).map(([k,v] as any )=>(Label:k, Description:v.subscribe.summary));
一样尝试过,但我得到了编译错误,我错过了什么吗?
像var result = Object.entries(obj.topics).map(([k,v]: any )=>(Label:k, Description:v.subscribe.summary));
一样修复它,你知道如果我想为TS使用显式类型,我应该如何使用它?【参考方案2】:
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 的内部数组,可能会更简洁一些
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)
【讨论】:
哪个错误? Object.entries 总是返回一个包含 [key, value] 数组的数组。您可以像 gorak 那样解构内部数组来访问它,或者使用 [0] 访问键并使用 [1] 访问值。也许您正在尝试的对象未定义 topic[1].subscribe?【参考方案3】:您可以映射主题中的所有键,并且对于每个项目,您可以从该对象中提取任何数据,如下所示:
Object.keys(obj.topics).map((key) => return Label: key, Description: obj.topics[key].subscribe.summary )
【讨论】:
【参考方案4】:你可以这样做
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)
【讨论】:
以上是关于从具有多个属性的数组映射 json 数据的主要内容,如果未能解决你的问题,请参考以下文章
Jquery从具有数组/ JSON值的多个数据属性中检索数据