0

我想为我的多维数组添加一个维度,称为description. 目标是遍历数组,计算子元素并根据存在的元素数量添加“父”维度description。但我目前正试图将信息附加到我的数组中。我想添加这个“父”维度,或者至少能够将字符串放入 location description[x]["TimeSeries"]["parent"]。有关如何进行的任何帮助?

(function () {
 var description;
 description = {
     1: {
         source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
         TimeSeries: {
             //parent: "#hero-three",
             title: "Clients Installed"
         }
     },
     2: {
         source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
         TimeSeries: {
             //parent: '#g2-1'
         }
     },
     3: {
         source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
         TimeSeries: {
             //parent: 
         }
     }
 };

 $(description[3]["TimeSeries"]["parent"]).append('g2-2');

 var g = new Graphene;
 g.build(description);
 alert(description[3]["TimeSeries"]["parent"]);
 }).call(this);
4

3 回答 3

1

这里有几点需要注意;所以希望这应该有助于改进您实现目标的方法,并在此过程中回答您的问题。

首先要考虑的是您没有使用多维数组,您实际上使用的是object. 数组的语法是var myArray = [];当你使用var myObject = {};. 这不一定是坏事......对象是好的!

据我所知,你实际上有一个array对象Description。如果是这种情况,您想像这样构建它......

var descriptions = [
    {
        source: '...',
        timeSeries: {
            parent: '...',
            title: '...'
        }
    },
    {
        source: '...',
        timeSeries: {
            parent: '...',
            title: '...'
        }
    }];

然后,您可以通过执行以下操作访问所需的信息:

for (var i = 0; i < descriptions.length; i++) {
    var current = descriptions[i];
    current.timeSeries.parent = '...';
}
于 2013-06-13T11:16:42.070 回答
0

你需要遍历你的对象。jquery 的 _.each函数可以帮助你做到这一点:

var description = {
    1: {
        source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
        TimeSeries: {
            //parent: "#hero-three",
            title: "Clients Installed"
        }
    },
    2: {
        source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
        TimeSeries: {
            //parent: '#g2-1'
        }
    },
    3: {
        source: "http://beat.entrayn.com/render?width=550&from=-2hours&until=-&height=570&colorList=FFFFFF%2C%20BBBBBB&target=*.entraynec2_ldb01.load.load5&target=*.entraynec2_ldb01.load.load1&title=DB%20EC2%20Load&_uniq=0.9870549130719155&format=json",
        TimeSeries: {
         //parent: 
        }
    }
};

var parents = ["parent1", "parent2", "parent3"];

$.each(description, function (key, value) {
    value["TimeSeries"]["parent"] = parents.shift();
});

console.log(description);

http://jsfiddle.net/yJKr9/2/或使用下划线:http: //jsfiddle.net/yJKr9/

于 2013-06-13T08:42:55.200 回答
0

代替:

$(description[3]["TimeSeries"]["parent"]).append('g2-2');

利用:

description[3]["TimeSeries"]["parent"] = 'Whatever you want';

在这里使用 jQuery 很奇怪。

还:

var g = new Graphene; //This is bad
var g = new Graphene(); //This is good

但是,我没有使用过石墨烯,所以我不知道它是否需要一些构造函数的参数。为了测试parent我从我的 jsfiddle 中删除了与石墨烯相关的行的正确分配:http: //jsfiddle.net/f9LaC/

于 2013-06-13T10:23:37.070 回答