I have an object:
object.day.time
where I need to access the time property.
Due to the nature of the function, I only have access to the base object. IE
function access(property){
let item = object[property]
// Do a lot of stuff with item
}
I don't want to rewrite the function because the main use case is accessing objects one level deep. IE the day property. Is there any way I can make this work?
The only thing I could think of was:
property = [['day']['time']]
but that didn't work.
EDIT: I originally had object as a param to access which was wrong. I that case I could just pass object.day as a value
You could do sometime like this to go to this time
property:
var object = { day: { time: (new Date()).getTime() } }; var properties = ["day","time"]; function access(object,properties){ for(var index=0; index < properties.length; index++){ // go to deeper into object until your reached time object = object[properties[index]]; } // here we have reached time and can do something with it or just returning it return object; } console.log(access(object,properties));
Hope this helps you.
调用此函数时,传递object.day而不是仅传递基础对象,这意味着函数中的对象已经是object.day,因此,如果执行object.time,则应获取该值。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.