根据父数组位置获取最后一个存在的子数组
Get the last existent sub array depending on parent array position
我将如何制作任何类型的 "for each" 以确保我将获得最后一个存在的子数组 属性。根据我的对象[索引],我可以有“1 或 10”个其他 "items" 子数组。
object[0].items[0].property(...)
object[1].items[0].items[0].items[0].property(...)
object[2].items[1].items[0].property(...)
object[3].items[1].property(...)
此致,
鲁本斯
您可以尝试编写一个递归函数,逐步遍历您的子数组,直到到达最低点,然后 return 属性。
类似于:
myRecFunc(obj) {
if ( obj.items && obj.items.lengh > 0 ) {
return myRecFunc(obj.items[0]);
}
return obj.property;
}
我将如何制作任何类型的 "for each" 以确保我将获得最后一个存在的子数组 属性。根据我的对象[索引],我可以有“1 或 10”个其他 "items" 子数组。
object[0].items[0].property(...)
object[1].items[0].items[0].items[0].property(...)
object[2].items[1].items[0].property(...)
object[3].items[1].property(...)
此致, 鲁本斯
您可以尝试编写一个递归函数,逐步遍历您的子数组,直到到达最低点,然后 return 属性。
类似于:
myRecFunc(obj) {
if ( obj.items && obj.items.lengh > 0 ) {
return myRecFunc(obj.items[0]);
}
return obj.property;
}