macek/jquery-serialize-object

Array with dot notation

nickforddev opened this issue · 3 comments

I changed the validate regex to look for dot notation (because my schema only understands dot notation for validation), but now it is not possible to create arrays.

The new regex: /^[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)*(?:\[\])?$/i

And here's an example of what I was previously looking to do:

test[test][][name]

How would this be achieved using dot notation? Would it be possible to combine the two validation regexes to allow for mixed notation?

macek commented

dot notation still supports the empty [] for push. So your key would be test.test[].name

Generally though, the push key is intended to be the last part of the key otherwise you're going to struggle working with it.


This following example wouldn't work. The push key would have no idea that age belongs to the first item in test.test and instead will always create a new element

test.test[].name
test.test[].age
test.test[].gender
test.test[].name
test.test[].age
test.test[].gender

Output

[
  {name: ...},
  {age: ...},
  {gender: ...},
  {name: ...},
  {age: ...},
  {gender: ...},
]

You're better using an incrementing index

test.test.0.name
test.test.0.age
test.test.0.gender
test.test.1.name
test.test.1.age
test.test.1.gender

Output

[
  {name: ..., age: ..., gender: ...},
  {name: ..., age: ..., gender: ...}
]
macek commented

Marking this as closed. Let me know if you have any other issues.

Thank you for clarifying this behavior, it works perfectly