合并的JSON JavaScript数组 [英] merging javascript arrays for json
问题描述
我连续收集形式的信息到像这样的数组:
I serially collect information from forms into arrays like so:
list = {"name" : "John", "email" : "name@domain.com", "country" : "Canada", "color" : "blue"};
identifier = "first_round";
list = {"name" : "Harry", "email" : "othername@domain.com", "country" : "Germany"};
identifier = "second_round";
我想将它们组合成的东西(也许是我,我需要括号大括号),如:
I want to combine them into something (I may have braces where I need brackets) like:
list_all = {
"first_round" :
{"name" : "John", "email" : "name@domain.com", "country" : "Canada", "color" : "blue"} ,
"second_round" :
{"name" : "Harry", "email" : "othername@domain.com", "country" : "Germany"}
};
这样我就可以像访问它们:
so I can access them like:
alert(list_all.first_round.name) -> John
(注:名称值(姓名,在两个列表阵列电子邮件,颜色)的不太一样,每个列表阵列中的项目数是有限的,但在不知道推进;我只需要一个阵列连续添加到previous结构每一轮有可能是任何数量的回合,即第三轮:{...},第四轮:{.. }等。)
(Note: the name-values ("name", "email", "color") in the two list-arrays are not quite the same, the number of items in each list-array is limited but not known in advance; I need to serially add only one array to the previous structure each round and there may be any number of rounds, i.e. "third-round" : {...}, "fourth-round" : {...} and so on.)
最后,我想它很好地解析的对JSON。
Ultimately, I'd like it to be well-parsed for JSON.
我用的是jQuery库,是否有帮助。
I use the jquery library, if that helps.
推荐答案
创建 list_all
作为新对象,如下所示:
Create list_all
as a new object as follows:
var list_all = {};
list_all[identifier_1] = list_1;
list_all[identifier_2] = list_2;
// ...
这篇关于合并的JSON JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!