Skip to content

Commit e6208be

Browse files
authored
add length var operation for objects (#5318)
1 parent 69ca396 commit e6208be

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

reflex/vars/object.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ def entries(self) -> ArrayVar:
144144

145145
items = entries
146146

147+
def length(self) -> NumberVar[int]:
148+
"""Get the length of the object.
149+
150+
Returns:
151+
The length of the object.
152+
"""
153+
return self.keys().length()
154+
147155
def merge(self, other: ObjectVar):
148156
"""Merge two objects.
149157

tests/integration/test_var_operations.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,15 @@ def index():
608608
int_var2=VarOperationState.int_var2,
609609
id="memo_comp_nested",
610610
),
611+
# length
612+
rx.box(
613+
rx.text(VarOperationState.list3.length()),
614+
id="list_length",
615+
),
616+
rx.box(
617+
rx.text(VarOperationState.obj.length()),
618+
id="obj_length",
619+
),
611620
# foreach in a match
612621
rx.box(
613622
rx.match(
@@ -944,6 +953,9 @@ def test_var_operations(driver, var_operations: AppHarness):
944953
# rx.memo component with state
945954
("memo_comp", "1210"),
946955
("memo_comp_nested", "345"),
956+
# length
957+
("list_length", "3"),
958+
("obj_length", "3"),
947959
# foreach in a match
948960
("foreach_in_match", "first\nsecond\nthird"),
949961
# literal range in a foreach

0 commit comments

Comments
 (0)