简体   繁体   中英

Hide a property name from output

I have a function that returns data. This data comes from an interface, with a property name. Output has the property name. I'd like to avoid that if possible.

How do I do this?

interface:

export interface DataNode {
    id?: number,
    type?: string,
    parentId?: number,
    data?: any,
    children?: DataNode[],
    buildings?: DataNode[],
    areas?: DataNode[],
    rooms?: DataNode[],
    flags?: DataNode[],
    details?: DataNode[]
};

Code that creates the output:

export const fromCity = async (
    city: entities.City,
    depth: "none" | "immediate" | "full"
): Promise<DataNode> => {
    return {
        id: city.id,
        type: "City",
        data: {
            name: city.name,
            title: city.title,
            // created: city.created,
            // updatedAt: city.updatedAt
        },
        buildings: depth !== "none" ?
            await mapChildren((building: entities.Building): Promise<DataNode> => {
                building.city = building.city || Promise.resolve(city);
                return fromBuilding(
                    building,
                    depth === "immediate" ? "none" : depth
                );
            })(await (city.buildings || Promise.resolve([]))) : []
    };
};

Actual output I get from API:

            "id": 1,
                "type": "Building",
                "parentId": 1,
                "data": {
                    "name": "data-center",
                    "title": "Data Center",
                    "subtitle": "Secure Data Center and Cloud NOC",
                    "x": "11.30",
                    "y": "41.50"

What I do want from the API:

            "id": 1,
                "type": "Building",
                "parentId": 1,
                {
                    "name": "data-center",
                    "title": "Data Center",
                    "subtitle": "Secure Data Center and Cloud NOC",
                    "x": "11.30",
                    "y": "41.50"

Note the disappearance of "data": . That's what I want to hide.

Is there a simple way to tell "don't return this property name, just it's value"?

An object's key must be a string. So the closest you'll get to your desired output would be naming the "data" key an empty string ""

Javascript doesn't let you do that. You can't have a value without a key to access it otherwise that data would be unreachable with an OBJECT. What you can do it make it output this with an Array:

[
  {
    "id": 1,
    "type": "Building"
    "parentId": 1
  },
  {
    "name": "data-center",
    "title": "Data Center",
    "subtitle": "Secure Data Center and Cloud NOC",
    "x": "11.30",
    "y": "41.50",
  }
]

Then you can access data from

JSON.parse(JSONresponse[1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM