简体   繁体   English

如何使用以下结构定义一个空的javascript对象,以便可以向其中动态插入数据?

[英]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.

相关问题 如何将变量设置为对象的键,以便可以使用javascript动态访问对象? - How do I set a variable to be a object's key so I can access an object dynamically in javascript? 如何在错字中定义JavaScript对象 - How can I define a javascript object in typoscript Javascript:我可以动态创建一个 CSSStyleSheet 对象并插入它吗? - Javascript: Can i dynamically create a CSSStyleSheet object and insert it? 我如何将数据从 javascript 插入到 php 表中,或者如何使用数据库而不是使用 localStorage 执行以下任务? - how do i insert data into a php table from javascript or How can i do the following task using database instead of using localStorage? 如何将javascript对象转换为纯文本,以便在发布数据时我可以发布简单文本 - How to convert javascript object to plain text so that while posting the data i can post simple text 如何从对象的数组中挑选数据并将其插入Javascript中的对象的另一个数组中? - How can I pick data out of an array in a object and insert it into another array in an object in Javascript? 如何在值中动态插入 javascript 变量? - How can i insert javascript variable in value dynamically? 如何通过 javaScript 中的“for of”循环将器官插入空数组 - how can I insert organs into an empty array by the "for of" loop in javaScript 如何使用jquery / javascript将值插入javascript对象 - How can I insert values in to javascript object by using jquery / javascript 如何翻译以下 JavaScript 以使其在 PHP 中完成? - How can I translate the following JavaScript so that its done in PHP instead?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM