[英]How to define an empty javascript object with the following struture so that I can dynamically insert data into it?
I am using JIT Infovis stacked or mono-valued piechart to do some data visualization. 我正在使用JIT Infovis堆叠或单值饼图进行一些数据可视化。 The stacked pie chart takes an object "obj" specifically with the following structure:
堆积的饼图采用以下结构,专门具有对象“ obj”:
This is the hard coded version of jsonObj: 这是jsonObj的硬编码版本:
var obj = {
// labelID is unique in each case and may be multiple or just one
'labelID': ['pVal'],
'values': [{ //labelName is unique in each case and will only be one
'labelName': 'name1',
//there may be multiple or just one "values"
//ex, 'values': [80, 40, 15, 10]
'values': 80
}, {
'labelName': 'name2',
'values': [20]
}, {
'labelName': 'name3',
'values': [38]
}, {
'labelName': 'name4',
'values': [58]
}]
};
I'm trying to dynamically populate "obj" with searched data returned to user. 我正在尝试使用返回给用户的搜索数据动态填充“ obj”。 But I can not create an empty "obj" with the above specific structure to dynamically populate the data.
但是我无法使用上述特定结构创建空的“ obj”来动态填充数据。 I tried several times but don't think I'm creating them correctly.
我尝试了几次,但认为自己创建的不正确。 I have three values that I'm trying to dynamically populate into this "obj" but can't get my arms around it.
我尝试将三个值动态填充到此“ obj”中,但无法解决。
chartID[m], chartArrName[m], chartVal[m]
. chartID[m], chartArrName[m], chartVal[m]
。
I need a correct empty "obj" that correspond to the structure defined above. 我需要与上面定义的结构相对应的正确的空“ obj”。
var "obj" = {
label: ['pVal'],
values: [{
label: [],
values: []
}]
};
for (m = 0; m <= chartArrID.length - 1; m++) {
obj.values[m].label += chartArrName[m];
obj.values[m].values += parseFloat(chartArrVal[m]);
//json.push({label: chartArrName[m], values: parseFloat(chartArrVal[m])});
}
This is not a JSON object. 这不是JSON对象。 JSON is a lightweight data interchange format.
JSON是一种轻量级的数据交换格式。 You are just using a object initializer syntax to create an object.
您仅使用对象初始化器语法创建对象。 You can add what you want of the fly.
您可以随意添加所需的内容。
var myObj = {};
myObj.name = "foo";
myObj.books = [ "one", "two" ];
myObj.emptyField = null;
myObj.emptyArray = [];
myObj["anotherField"] = "bar"; // this works too
myObj.anotherArray = [];
myObj.anotherArray[0] = {}; // you need to insert something in the first position!
myObj.anotherArray[0].label = "aaa";
myObj.anotherArray[0].value = "bbb";
myObj.anotherArray[0].xyz = "ccc";
myObj.anotherArray[1] = {}; // in the second too, of course!
myObj.anotherArray[1].label = "ddd";
myObj.anotherArray[1].value = "eee";
myObj.anotherArray[1].xyz = "fff";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.