[英]Create an object with custom structure in javascript
I need to create an array object with this exact structure: 我需要创建一个具有以下确切结构的数组对象:
{"item1": {
0 : {
color:"description1",
width:"description2",
height:"description3"
}
1 : {
color:"description1",
width:"description2",
height:"description3"
}
//and so on
},
"item2": {
0 : {
color:"description1",
width:"description2",
height:"description3"
}
//and so on
}
//and so on
}
Estatically works fine. 静态地工作正常。 Now, I want to make it dinamically.
现在,我要使它更具特色。 So, the main question is... How can I loop the data while constructing the object at the same time?
因此,主要问题是...在构造对象的同时如何循环数据? This is an example of the incoming object I work from:
这是我工作的传入对象的示例:
[
{
"uid": 1,
"legendname": "item1",
"rows": [
{
"uid": 0,
"color": "482400",
"width": "482400",
"height": "25"
},
{
"uid": 1,
"color": "587898",
"width": "789658",
"height": "30"
}
]
}
{
"uid": 2,
"legendname": "item2",
"rows": [
{
"uid": 0,
"color": "482400",
"width": "482400",
"height": "25"
}
]
}
]
How can I loop the data while constructing the object at the same time? 如何在构造对象的同时循环数据?
Is not clear. 不清楚。 But if you want to create a new structure based on the incoming one, you can use this:
但是,如果要基于传入的结构创建一个新结构,则可以使用以下方法:
// ar = your incoming object
// b_obj = your result
var b_obj = {}
for (var i = 0; i < ar.length; i++)
{
item = ar[i];
b_item = {};
for (var i_row = 0; i_row < item.rows.length; i_row++)
{
var row = {};
row.color = item.rows[i_row].color;
row.width = item.rows[i_row].width;
row.height = item.rows[i_row].height;
b_item[i_row] = row;
}
b_obj[item.legendname] = b_item;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.