使用多个属性映射数组中的 json 数据

Ben*_*Odr 6 javascript json node.js typescript lodash

I\xe2\x80\x99ve 以下 json 对象

\n\n

https://codebeautify.org/jsonviewer/cb01bb4d

\n\n
obj = \n{\n  "api": "1.0.0",\n  "info": {\n    "title": "Events",\n    "version": "v1",\n    "description": "Set of events"\n  },\n  "topics": {\n    "cust.created.v1": {                            //Take this value\n      "subscribe": {\n        "summary": "Customer Register Event v2",    //Take this value\n        "payload": {\n          "type": "object",\n          "required": [\n            "storeUid"\n\n          ],\n          "properties": {\n            "customerUid": {\n              "type": "string",\n              "description": "Email of a Customer",\n              "title": "Customer uid"\n            }\n          }\n        }\n      }\n    },\n    "qu.orderplaced.v1": {                     //Take this value\n      "subscribe": {\n        "summary": "Order Placed",             //Take this value\n        "payload": {\n          "type": "object",\n          "required": [\n            "quoteCode"\n          ],\n          "properties": {\n            "quoteCode": {\n              "type": "string",\n              "example": "762",\n              "title": "Quote Code"\n            }\n          }\n        }\n      }\n    } \n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我需要将 json 对象中的值映射到 javascript 数组

\n\n

例如:

\n\n
MyArray = [\n {\n   Label: \xe2\x80\x9ccust.created.v1\xe2\x80\x9d,\n   Description:  "Customer Register Event v2"\n\n },\n {\n   Label: \xe2\x80\x9cqu.orderplaced.v1\xe2\x80\x9d,\n   Description: "Order Placed",\n }\n]\n
Run Code Online (Sandbox Code Playgroud)\n\n

我需要映射两个值,每个实例的键 => 标签(例如 \xe2\x80\x9ccust.created.v1\xe2\x80\x9d )和每个实例的摘要 => 描述

\n\n

我\xe2\x80\x99ve尝试用map来做到这一点,但我很难用key和里面的属性来做到这一点,是否可以用map来做到这一点?

\n

Raj*_*jan 6

获取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);
Run Code Online (Sandbox Code Playgroud)