简体   繁体   English

向多维数组JS添加维

[英]add dimension to multidimensional array JS

I want to add a dimension to my multidimensional array called description . 我想在多维数组中添加一个名为description的维度。 The objective is to go through the array, count the children and add a "parent" dimension based on the number of elements existing in description . 目的是遍历数组,对子对象进行计数,并根据description存在的元素数量添加一个“父”维度。 But i'm currently stuck trying to append information into my array. 但是我目前一直在尝试将信息追加到数组中。 I want to add this "parent" dimension, or at least be able to put a string into the location description[x]["TimeSeries"]["parent"] . 我想添加此“父”维度,或至少能够将字符串放入位置description[x]["TimeSeries"]["parent"] Any help on how to proceed? 对如何进行有帮助吗?

(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);

There are a few things to note here; 这里有几件事要注意; so hopefully this should help improve your approach to what you're trying to achieve, and also answer your question in the process. 因此希望这将有助于改进您要达到的目标的方法,并在此过程中回答您的问题。

The first thing to consider is that you're not using a multi-dimensional array, you're actually using an object . 首先要考虑的是,您没有使用多维数组,而是实际上使用了object The syntax for an array is var myArray = []; 数组的语法为var myArray = []; whereas you're using var myObject = {}; 而您正在使用var myObject = {}; . This isn't necessarily a bad thing... objects are good! 这不一定是一件坏事……对象是好东西!

From what I can gather, you've actually got an array of Description objects. 据我所知,您实际上有一个Description对象array If this is the case you'd want to construct it like so... 如果是这种情况,您希望像这样构造它...

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

You can then access your required information by doing the following: 然后,您可以通过执行以下操作访问所需的信息:

for (var i = 0; i < descriptions.length; i++) {
    var current = descriptions[i];
    current.timeSeries.parent = '...';
}

You need to walk through your object. 您需要遍历您的对象。 jquery's _.each function can help you doing this: 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/ or using underscore each : http://jsfiddle.net/yJKr9/ http://jsfiddle.net/yJKr9/2/或使用下划线http : //jsfiddle.net/yJKr9/

Instead of: 代替:

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

Use: 采用:

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

Using jQuery here is weird. 在这里使用jQuery很奇怪。

Also: 也:

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

However, I haven't used Graphene, so I don't know if it requires some arguments for constructor. 但是,我还没有使用Graphene,所以我不知道它是否需要一些构造函数参数。 To test proper assignment of parent i removed Graphene related lines from my jsfiddle: http://jsfiddle.net/f9LaC/ 为了测试parent正确分配,我从jsfiddle中删除了与Graphene相关的行: http : //jsfiddle.net/f9LaC/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM