js根据规则替换json中所有key值

TeamWei 2021-01-28 11:42:23
自己写了半天还是无法实现,最多只能替换一层的key,多层太难了


let result = {};
Object.keys(property).forEach(key => {
let value = property[key];

if ({}.toString.call(value) === '[object Object]') {
value = Object.keys(property[key])[0];
}

if (data.hasOwnProperty(value)) {
result[key] = data[value];
}
})


需要替换的规则

const property = {
userId: 'user_id',
userName: 'user_name',
product: {
'order_attrs': {
attrId: 'id',
attrName: 'attr_name',
attrValue: 'attr_value'
}
},
userAddress: {
'address': {
region: 'region',
avatar: 'image_url',
phoneNumber: {
'phone': {
one: 'a',
two: 'b'
}
}
}
}
}


需要替换的数据

const data = {
user_id: '652',
user_name: 'csdn',
order_attrs: [{
id: 345,
attr_name: '型号',
attr_value: 'iphone'
},
{
id: 1897,
attr_name: '容量',
attr_value: '256G'
}
],
address: {
region: '北京市',
image_url: '/xxx/aaa.jpg',
phone: {
a: 'eeee',
b: 'ccc'
}
}
}


替换的结果

const result = {
userId: '652',
userName: 'csdn',
product: [{
attrId: 345,
attrName: '型号',
attrValue: 'iphone'
},
{
attrId: 1897,
attrName: '容量',
attrValue: '256G'
}
],
userAddress: {
region: '北京市',
imageUrl: '/xxx/aaa.jpg',
phoneNumber: {
one: 'eeee',
two: 'ccc'
}
}
}
...全文
434 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
TeamWei 2021-01-28
  • 打赏
  • 举报
回复
let subPro = pro[key];
这里有点问题,不过自己解决了
TeamWei 2021-01-28
  • 打赏
  • 举报
回复
引用 1 楼 qybao 的回复:
这种层次嵌套一般用递归
const property = {
    userId: 'user_id',
    userName: 'user_name',
    product: {
        'order_attrs': {
            attrId: 'id',
            attrName: 'attr_name',
            attrValue: 'attr_value'
        }
    },
    userAddress: {
        'address': {
            region: 'region',
            avatar: 'image_url',
            phoneNumber: {
                'phone': {
                    one: 'a',
                    two: 'b'
                }
            }
        }
    }
}

const data = {
    user_id: '652',
    user_name: 'csdn',
    order_attrs: [{
        id: 345,
        attr_name: '型号',
        attr_value: 'iphone'
    },
        {
            id: 1897,
            attr_name: '容量',
            attr_value: '256G'
        }
    ],
    address: {
        region: '北京市',
        image_url: '/xxx/aaa.jpg',
        phone: {
            a: 'eeee',
            b: 'ccc'
        }
    }
}

const replaceKey = (src, pro) => {
    let res = {};
    Object.keys(pro).forEach(key=>{
        let val = pro[key];
        if ({}.toString.call(val) === '[object String]') {
            if (src.hasOwnProperty(val)) {
                res[key] = src[val];
            }
        } else if ({}.toString.call(val) === '[object Object]') {
            let subPro = pro[key];
            if (src.hasOwnProperty(key)) {
                if (Array.isArray(src[key])) {
                    let arr = [];
                    src[key].forEach(e=>{
                        arr.push({...replaceKey(e, subPro)});
                    });
                    res = arr;
                } else {
                    res = replaceKey(src[key], subPro);
                }
            } else {
                res[key] = replaceKey(src, subPro);
            }
        }
    })
    return res;
}

console.log(replaceKey(data, property));

解决了,我还得再多学习一下
qybao 2021-01-28
  • 打赏
  • 举报
回复
这种层次嵌套一般用递归
const property = {
    userId: 'user_id',
    userName: 'user_name',
    product: {
        'order_attrs': {
            attrId: 'id',
            attrName: 'attr_name',
            attrValue: 'attr_value'
        }
    },
    userAddress: {
        'address': {
            region: 'region',
            avatar: 'image_url',
            phoneNumber: {
                'phone': {
                    one: 'a',
                    two: 'b'
                }
            }
        }
    }
}

const data = {
    user_id: '652',
    user_name: 'csdn',
    order_attrs: [{
        id: 345,
        attr_name: '型号',
        attr_value: 'iphone'
    },
        {
            id: 1897,
            attr_name: '容量',
            attr_value: '256G'
        }
    ],
    address: {
        region: '北京市',
        image_url: '/xxx/aaa.jpg',
        phone: {
            a: 'eeee',
            b: 'ccc'
        }
    }
}

const replaceKey = (src, pro) => {
    let res = {};
    Object.keys(pro).forEach(key=>{
        let val = pro[key];
        if ({}.toString.call(val) === '[object String]') {
            if (src.hasOwnProperty(val)) {
                res[key] = src[val];
            }
        } else if ({}.toString.call(val) === '[object Object]') {
            let subPro = pro[key];
            if (src.hasOwnProperty(key)) {
                if (Array.isArray(src[key])) {
                    let arr = [];
                    src[key].forEach(e=>{
                        arr.push({...replaceKey(e, subPro)});
                    });
                    res = arr;
                } else {
                    res = replaceKey(src[key], subPro);
                }
            } else {
                res[key] = replaceKey(src, subPro);
            }
        }
    })
    return res;
}

console.log(replaceKey(data, property));

87,987

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧