Data interceptor: recursively traverse objects/arrays, and callbacks provide modification interfaces for properties and values

/**
 * Data interceptor:
 *
 * Perform deep recursion on complex type data, provide modified interception attributes, and process attributes, values, etc. in the callback
 *
 * @param {*} data object/array The target (array or object) to be intercepted
 * @param {*} callback1 function The callback function internally does something to the value of the innermost layer (value is the basic type, no nesting), and the callback parameter is an obj
 * @param {*} callback2 function Same as above, the difference is that value is a complex type with nesting
 * Callback parameter description:
 *      {
            type: 'Object',// 'Object'|'Array' type of parent node
            obj: data, // parent node object (reference)
            key: key, // current key (ps: type is 'Array' is the current index)
            value: value // current value
        }
 * Application scenario 1:
        When you need to deal with special data in your project, such as testing that the page cannot appear null!
        At this point you can call this function before data rendering and do some processing on the value in callback1

    Application scenario 2 (the purpose of originally writing this function):
        Personalize the options of echarts, data supports a variety of configuration methods, different methods
        Do different processing, use this function to do some processing on data in callback2 and then setOption
 */
function dataInterceptor(data, callback1, callback2) {

    again(data)

    function again(data) {

        if (type(data) === 'Object') {

            for ( var key in data) {
                 // console.log('key------',key) ---- all keys go through here 
                if ( typeof data[key] === "object" ) {
                     //The parent node is an object, the value is a complex type 
                    if (callback2) callback2({
                        type: 'Object',
                        obj: data,
                        key: key,
                        value: data[key]
                    })
                    again(data[key])
                } else {
                     // console.log(data[key]) 
                    //The   parent node is the object, the value is the basic type 
                    if (callback1) callback1({
                        type: 'Object',
                        obj: data,
                        key: key,
                        value: data[key]
                    })
                }
            }

        } else if (type(data) === 'Array') {
            data.forEach( function (item, index) {
                 if ( typeof item === "object" ) {
                     // The parent node is an array, the value is a complex type 
                    if (callback2) callback2({
                        type: 'Array',
                        obj: data,
                        key: index,
                        value: item
                    })
                    again(item)
                } else {
                     //   The parent node is an array, the value is the basic type--->Each item of the array 
                    // console.log(item) 
                    if (callback1) callback1({
                        type: 'Array',
                        obj: data,
                        key: index,
                        value: item
                    })
                }
            })
        }
    }

    function type(data) {
        return Object.prototype.toString.call(data).slice(8, -1)
    }
}

 

Reprinted in: https://www.cnblogs.com/superjsman/p/11557901.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568866&siteId=291194637