有一个像这样的对象:
现在我希望能够循环遍历给定的字符串,该字符串代表了一个特定路径(props.intro.content),以便将最深处的值设置为undefined:
props = {
any: 'thing',
intro: { content: 'foo' }
}
现在我希望能够循环遍历给定的字符串,该字符串代表了一个特定路径(props.intro.content),以便将最深处的值设置为undefined:
props.intro.content = undefined
props.intro = undefined
props = undefined
通过迭代上述路径得到的结果应输出以下三个对象:
{
any: 'thing',
intro: { content: undefined }
},
{
any: 'thing',
intro: undefined
},
undefined
我尝试使用分割和循环
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()
}
但是这种方法并没有处理对象本身,因此输出的结果不正确。