1

我想在循环内创建具有这种结构的 json:

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

我希望它是一个变量 id_foto

以便:

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' },] }
}

最终结果如:

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

然后取 11 个索引的所有用户

4

1 回答 1

0

为您的数据实现顺序 id 的一种方法是创建:-
1. 存储您的 id 的当前值的地方,
2. 增加和返回您的序列号的函数

您可以像这样将当前 id 值存储在数据对象中:-

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

然后使用以下内容来增加并返回下一个序列 id

function id_foto() {
   return ++your_object.seq;//get,increment and return incremented value
}
于 2013-08-17T16:48:53.470 回答