有以下的简单对象
numbers:{
one:'one',
two:'two',
three:'htree',
fourth:'fourth'
}
我想通过判断存在的形式依次转为下面的代码
if(numbers.fourth){
let num = {
[fourth]:{
[three]:{
[two]:{
[one]:'ok'
}
}
}
}
}else if(numbers.three){
let num = {
[three]:{
[two]:{
[one]:'ok'
}
}
}
}else if(numbers.two){
...
这里就不写了,一样的参数
}
numbers对象里的属性是可变的,所以需要动态判断并生成相应结构的对象,怎么实现好点(尽量减少代码量)?
求各位路过的大神么,及热心吃瓜群众们!!!
属性个数可变 属性名不变
如以下代码: