1

I'd like create json with this structure inside the cycle:

{ id_foto: 
   [ { firstName: 37, lastName: 'Doe' },
     { firstName: 'Anna', lastName: 'Smith' },
     { firstName: 'Peter', lastName: 'Jones' } ] }

I wish it were a variable id_foto

so that:

if (id_foto == n.foto_moderata) {
 // add new { firstName: 'Anna', lastName: 'Smith' }
 } else {
 // create new "node" like
{ id_foto(NEW INDEX): 
       [ { firstName: 37, lastName: 'Doe' },] }
}

The Final result like:

{ 10: 
   [ { firstName: 37, lastName: 'Doe' },
     { firstName: 'Anna', lastName: 'Smith' },
     { firstName: 'Peter', lastName: 'Jones' } ]
11: 
   [ { firstName: fff, lastName: 'fff' },
     { firstName: 'fff', lastName: 'fff' } ]
 }

Then take all user of 11 index

Cœur
  • 37,241
  • 25
  • 195
  • 267
Barno
  • 3,271
  • 5
  • 28
  • 58
  • Can't do it directly in the object literal syntax. You need to create the object, then add the new property with the dynamic name after. If you don't know how to create a property name from a variable, just look at some of the hundreds of other questions on StackOverflow that have asked how. –  Aug 17 '13 at 16:47
  • i see this post http://stackoverflow.com/questions/4538269/adding-removing-items-from-json-data-with-jquery, but i'd like create many item – Barno Aug 17 '13 at 17:41

1 Answers1

0

One way to achieve a sequential ids for your data is to create:-
1. a place to store the current value of your id,
2. a function to increment and return your serial

You could store the current id value in your data object like so:-

{ 
   seq : 11,
   10: 
   [ { firstName: 37, lastName: 'Doe' },
     { firstName: 'Anna', lastName: 'Smith' },
     { firstName: 'Peter', lastName: 'Jones' } ],
   11: 
   [ { firstName: fff, lastName: 'fff' },
     { firstName: 'fff', lastName: 'fff' } ]
}

and then use the following to increment & return next sequence id

function id_foto() {
   return ++your_object.seq;//get,increment and return incremented value
}
mrmoje
  • 3,714
  • 3
  • 20
  • 18