将维度添加到多维数组JS

时间:2013-06-13 08:15:38

标签: javascript jquery

我想为名为description的多维数组添加维度。目标是遍历数组,计算子项并根据description中存在的元素数添加“父”维。但我目前仍然试图将信息附加到我的阵列中。我想添加这个“父”维度,或者至少能够将字符串放入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);

3 个答案:

答案 0 :(得分:1)

这里有几点需要注意;所以希望这有助于改善你想要实现的目标,并在此过程中回答你的问题。

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

根据我的收集,你实际上得到了arrayDescription个对象。如果是这种情况,你想要像这样构建它......

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 = '...';
}

答案 1 :(得分:0)

你需要走过你的对象。 jquery's _.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/或使用underscore eachhttp://jsfiddle.net/yJKr9/

答案 2 :(得分: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

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