有一个类似以下对象的对象:
props = {
any: 'thing',
intro: { content: 'foo' }
}
现在,我想遍历表示specific路径(props.intro.content
)的给定字符串,将最深的值设置为undefined
:
props.intro.content = undefined
props.intro = undefined
props = undefined
因此,通过迭代上面给出的路径得到的结果应该输出以下三个对象:
{
any: 'thing',
intro: { content: undefined }
},
{
any: 'thing',
intro: undefined
},
undefined
我try 使用Split和For循环
const array = 'props.intro.content'.split('.')
for (let index = array.length - 1; index > -1; index--) {
console.log([array.join('.')]) // this returns the flatten path
array.pop()
}
但这并不处理对象本身,所以我没有得到正确的对象作为输出.