简体   繁体   中英

Include all object properties with JSON.stringify()'s replacer array

I have this object, which we'll call JSONchunk :

{
    "stuffIWant": {
        "number": 123,
        "string": "string",
        "boolean": true
    },
    "boringDumbStuff": {
        "number": 456,
        "boolean": false
    }
}

I want to get every property of stuffIWant . However, I don't know all the properties that stuffIWant will contain at a given time.

I was hoping to use JSON.stringify(JSONchunk, ['stuffIWant']) but I only get part of the output I want. Namely, it returns {stuffIWant: {}} . Close, but no ciggie.

EDIT: Here's the result I'm looking for.

{
    "stuffIWant": {
        "number": 123,
        "string": "string",
        "boolean": true
    },
}

Is there a way to use a replacer array to return an object AND all of its properties without explicitly referencing those properties in the array? Or will I need to craft something a little more sophisticated?

简单:

JSON.stringify(JSONchunk.stuffIWant)

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