javascript多级对象操作

javascript多级对象操作,javascript,arrays,object,Javascript,Arrays,Object,如何编写接受injectionObject并修改currentObject的函数?如果我没有弄错的话,我相信这就是GraphQL的工作方式。我需要在纯JS中实现这个效果 //Object to inject into current object const injectionObject = { pages: { about: { title: 'Changed About Page'

如何编写接受injectionObject并修改currentObject的函数?如果我没有弄错的话,我相信这就是GraphQL的工作方式。我需要在纯JS中实现这个效果

    //Object to inject into current object
    const injectionObject = {
        pages: {
            about: {
                title: 'Changed About Page'
            }
        }
    }
    //Object that requires modification based on above object
    const currentObject = {
        pages: {
            home: {
                title: 'Home Page',
                description: 'This is the home page'
            },
            about: {
                title: 'About Page',
                description: 'This is the about page'
            }
        }
    }
    //output
    const outputObject = {
        pages: {
            home: {
                title: 'Home Page',
                description: 'This is the home page'
            },
            about: {
                title: 'Changed About Page', //Only this was affected
                description: 'This is the about page'
            }
        }
    }

您可以获取条目并更新属性或访问嵌套属性

const
更新=(目标,源)=>Object.entries(源).forEach([k,v])=>{
if(v&&typeof v==='object')更新(target[k]=target[k]|{},v);
else目标[k]=v;
}),
injectionObject={pages:{about:{title:'Changed about Page'}}},
currentObject={pages:{home:{title:'home Page',description:'这是主页'},about:{title:'about Page',description:'这是about页面'}};
更新(currentObject、injectionObject);
console.log(currentObject)
。作为控制台包装{最大高度:100%!重要;顶部:0;}