简体   繁体   English

用数组数据循环javascript

[英]Looping with array data javascript

I'm new to javascript, and I'm having trouble figuring out how to loop through some code so that it will basically create an array that I can then pass on to my plot variable. 我是javascript新手,在弄清楚如何循环执行某些代码时遇到了麻烦,因此它基本上会创建一个数组,然后将其传递给我的plot变量。

I'm not really sure where to start. 我不太确定从哪里开始。 Right now I have a chunk of code that takes my first dataset (dataOne) and formats it so that it can go into my plot variable. 现在,我有一大堆代码可以获取我的第一个数据集(dataOne)并对其进行格式化,以便可以将其放入我的plot变量中。 I basically need to do that three more times for the other data sets - hoping to include the example.getDataSets function to loop through somehow. 对于其他数据集,我基本上需要再执行三遍-希望包括example.getDataSets函数以某种方式循环遍历。

Is there a good way to do this? 有什么好方法吗?

Here is my code: 这是我的代码:

script.js script.js

var example = {};

example.data = {
    dataOne: {data: [{"date":1333238400000,"data":23},{"date":1333324800000,"data":37},{"date":1333411200000,"data":49},{"date":1333497600000,"data":54},{"date":1333584000000,"data":30},{"date":1333670400000,"data":19},{"date":1333756800000,"data":15},{"date":1333843200000,"data":19},{"date":1333929600000,"data":145}]},
    dataTwo: {data: [{"date":1335830400000,"data":63},{"date":1335916800000,"data":77},{"date":1336003200000,"data":66}]},
    dataThree: {data: [{"date":1341100800000,"data":24},{"date":1341187200000,"data":50},{"date":1341273600000,"data":43},{"date":1341360000000,"data":39},{"date":1341446400000,"data":56},{"date":1341532800000,"data":66}]},
    dataFour: {data: [{"date":1333238400000,"data":71},{"date":1333324800000,"data":46},{"date":1333411200000,"data":66},{"date":1333497600000,"data":73},{"date":1333584000000,"data":105},{"date":1333670400000,"data":84}]}
}

example.getDataSets = function(){
    return ['dataOne', 'dataTwo', 'dataThree', 'dataFour']
}

example.getSeries = function(month){
    return example.data[month]
}

example.processData = function(data){
    var newData = []
    for(var i = 0; i < data.length; i++){
        newData.push([data[i].date, data[i].data])
    };
    return newData;
}

My script in the HTML page: 我在HTML页面中的脚本:

$.getScript("script.js")
    .done(function() {

    var b = example.getSeries('dataOne');
    var d = example.processData(b.data);
    // first correct the timestamps - they are recorded as the daily
    // midnights in UTC+0100, but Flot always displays dates in UTC
    // so we have to add one hour to hit the midnights in the plot
    for (var i = 0; i < d.length; ++i)
      d[i][0] += 60 * 60 * 1000;

    var plot = $.plot($("#placeholder"), [d] , options);

Any suggestions are much appreciated! 任何建议,不胜感激!

Loop over the value returned from .getDataSets , too 也循环从.getDataSets返回的值

var sets = example.getDataSets(), set_i = 0, // get list of sets
    b = {}, d = [], i = 0, plot; // set-up vars
for(; set_i < sets.length; set_i++){ // loop over each set
    b = example.getSeries( sets[ set_i ] ); // get your set
    d = example.processData(b.data);
    for (i = 0; i < d.length; ++i)
      d[i][0] += 60 * 60 * 1000;

    plot = $.plot($("#placeholder"), [d] , options);
    // ... etc with plot
}

If you want them all in one array before plotting, use concat on another array (call it d_total or something) and then in the set loop, d_total = d_total.concat( d ); 如果要在绘制前将它们全部放在一个数组中,请在另一个数组上使用concat (称其为d_total或其他名称),然后在set循环中使用d_total = d_total.concat( d ); , then plot outside loop ,然后画外循环

I'm assuming you want to process each of 我假设您要处理每个

var b = example.getSeries(X);

where X is, in turn, 'dataOne', 'dataTwo', 'dataThree', 'dataFour'? X在哪里依次是“ dataOne”,“ dataTwo”,“ dataThree”,“ dataFour”?

If so, you'll be looking for something like this: 如果是这样,您将寻找这样的东西:

// see below for where example.ordering is suggested
for (var i in example.ordered) {
  var month = example.ordered[i];
  var b = example.getSeries(month);
  var d = example.processData(b.data);
  ... further processing with month, b, d.
}

The getDataSets() is fine for yielding your 'months' in what must be proper order for you. getDataSets()适合按必须适合您的顺序产生“月”。 My only concern is that you have the list of months in two places. 我唯一关心的是您在两个地方都有月份列表。 The array you have is necessary for ordering the months, because you cannot extract the 'month names' from example.data and expect them to be in the same order (keys are essentially stored randomly due to hashing). 您拥有的数组是订购月份所必需的,因为您无法从example.data中提取“月份名称”并期望它们具有相同的顺序(由于散列,键本质上是随机存储的)。

The qualification would be if your keys are sortable, but the keys you have here are not. 限定条件是您的钥匙是否可以排序,但这里没有钥匙。 Of course, if you're building the pair of structures from the same source, then storing the data in the associated array (example.data object) along with a separate array to indicate ordering is both common and acceptable. 当然,如果要从同一源构建一对结构,则将数据与单独的数组一起存储在关联的数组(example.data对象)中以指示顺序是常见且可接受的。 Ideally you'd pair these two into yet another object so they can be handled as a team. 理想情况下,请将这两个对象配对到另一个对象中,以便可以作为一个团队来处理它们。

Since your array of ordered months is literally 'lost' in a function, I would suggest you add something like 由于您订购的月份数组实际上是在函数中“丢失”,我建议您添加以下内容

example.ordering = [ ..... ]

and even better, push the months onto the end of that list in the same order they are put into the hash. 甚至更好的是,以将它们放在哈希表中的顺序将月份推到该列表的末尾。 I would be using a helper function to store the data: 我将使用一个辅助函数来存储数据:

example.ordering = [];  // initially empty
example.storeSet = function (month, dataList) {
    example.ordering.push(month);
    example.data[month] = { data : dataList };
}

....
// now store the data
example.storeSet('dataOne',   [{"date":1333238400000,"data":23}, ....]);
example.storeSet('dataTwo',   [{"date":1335830400000,"data":63}, ....]);
example.storeSet('dataThree', [{"date":1341100800000,"data":24}, ....]);
example.storeSet('dataFour',  [{"date":1333238400000,"data":71}, ....]);

Of course my code is untested (of course!) but it looks right, and I'm sure you can fix any stupid mistakes I might have made. 当然,我的代码未经测试(当然!),但是看起来不错,而且我相信您可以解决我可能犯的任何愚蠢的错误。

I am new and understand your frustration when you have sat and looked and still cannot find the answer. 我是新来的,当您坐下来看了看仍然找不到答案时,您会感到沮丧。 However, what I think you are looking for is something like this: 但是,我认为您正在寻找的是这样的东西:

for (example.data=dataOne;example.data<dataFour;example.data=example.data+increment)
{
    code to be executed
}

or maybe even recursively but I am not the best at that but below is an standard example 甚至递归,但我并不是最擅长的,但下面是一个标准示例

function recurs(p1) {
    p1--;
    if (p1 < 0) return // exit condition
        setTimeout("recurs("+p1+")",1);
}

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

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