const parse = data => {
    const uid = `uid_${Date.now()}`;
    const process = (input, prefix = '', json = {}) => {
        Object.keys(input).forEach(key => {
            const val = input[key];

            if (!json.hasOwnProperty(`${prefix}${key}.`)) {
                json[`${prefix}${key}.`] = uid;
            }

            if ('object' === typeof val && !Array.isArray(val) && !!val) {
                process(val, `${prefix}${key}.`, json);
            }
            else {
                json[`${prefix}${key}.`] = val;
            }
        });

        return json;
    };

    const json = process(data);
    const result = {};
    Object.keys(json).forEach(key => {
        if (json[key] !== uid) {
            result[key.slice(0, -1)] = json[key];
        }
    });
    return result;
};

posted on 2022-03-15 19:53  ljyyjj  阅读(1102)  评论(0编辑  收藏  举报