[JavaScript] JSON.stringify() The second parameter is anti-pit (filtering object properties by array matching)

Hits: 0

[JavaScript Tutorials / Standard Library / JSON Objects / JSON.stringify() / Second Parameter] / Second Parameter](https://wangdoc.com/javascript/stdlib/json.html#%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%8F%82%E6%95%B0)

1. JSON.stringify() method implements [array] method implements [array] filtering object properties [pit]

Object.prototype.screen = function(arr) {
    var result = JSON.stringify(this, arr);
    return JSON.parse(result);
}

Simple data can still be processed: (shallow data)
but more complex data cannot be processed: (deep data)
Reason: [mainly for deep objects]

2. Use other methods to achieve screening

Object.prototype.screen = function(arr) {
    var result = {};
    for (var item in this) {
        if (arr.indexOf(item) > -1) {
            result[item] = this[item];
        }
    }
    return result;
}

However, please note:

You may also like...

Leave a Reply

Your email address will not be published.