简体   繁体   English

在函数之间循环并存储结果

[英]Looping between functions and storing results

I would like produce a list of grouped JSON elements according to a specific criteria, but I am unable to make my loop work. 我想根据特定条件生成一个分组的JSON元素列表,但无法使循环正常工作。 The function should make groups of with 12 bottles and return a single JSON list. 该函数应以12个瓶子为一组,并返回一个JSON列表。 So in this example, the function should extract the 3 first items and then run again to extract the remaining ones. 因此,在此示例中,该函数应提取前三个项目,然后再次运行以提取其余项目。 But I am looping forever... Thank you in advance, 但我一直在循环……谢谢你,

var data = {
    "order": [
        { "product": "MAXIMUS", "quantity": "3" },
        { "product": "COLECCION", "quantity": "3" },
        { "product": "CABERNET FRANC", "quantity": "6" },
        { "product": "CHARDONNAY", "quantity": "6" },
        { "product": "SAUVIGNON BLANC", "quantity": "6" }
    ]
};

var qtd = data.order;
var size = qtd.length;
var addline = '';
var add = '';
var total = 0;
var i = 0;
var a = 0;
var c = '';

function MakeList(i, add) {
    for (i < 0; total < 12; i++) {
        total += parseInt(qtd[i].quantity);
        addline = addline + '{' + '"quantity": "' + qtd[i].quantity + ' units"},';
        i = i++;
        add = '{"Box of 12":[' + addline.slice(0, -1) + "]}";
    }
    return [i, add];
}

function BuildLabels(i, add) {
    for (i < 0; c = "true"; i++) {
        c = a[0] < size;
        a += MakeList(i, add);
        i = i++;
    }
    return a;
}

var results = BuildLabels(i, add);
output = { id: 3, results };
for (i < 0; c = "true"; i++)

something weird is happening here. 这里发生了一些奇怪的事情。 You don't set any condition on cycle to stop, you just assign value "true" to c . 您无需设置任何条件来停止循环,只需将值“ true”分配给c Try to use == instead of = ; 尝试使用==代替= ; also initialization looks strange - set i to 0 . 初始化看起来也很奇怪-将i设置为0 Apparently, It will make the whole thing work (at least the loop will stop at some point), but in the end I get that the variable results is equal to 0 . 显然,这将使整个过程正常工作(至少循环将在某个点停止),但是最后我得到变量results等于0 There are other mistakes/weird stuff out there. 还有其他错误/奇怪的东西。 Propably, you wanted to achieve something like this: 大概,您想实现以下目标:

 var data = { "order": [ { "product": "MAXIMUS", "quantity": "3" }, { "product": "COLECCION", "quantity": "3" }, { "product": "CABERNET FRANC", "quantity": "6" }, { "product": "CHARDONNAY", "quantity": "6" }, { "product": "SAUVIGNON BLANC", "quantity": "6" } ] }; function MakeList(data) { var selected = [], bottlesNum = 0; for (var i = 0; bottlesNum < 12; i++) { selected.push(data.order[i]); bottlesNum += parseInt(data.order[i].quantity); } return selected; } var results = MakeList(data); // now it is a JS object: console.log({ id: 3, results: results }); // if you want it to be a JSON string, use JSON.stringify(): console.log(JSON.stringify({ id: 3, results: results })); 

check it out. 看看这个。

UPDATE UPDATE

 var data = { "order": [ { "product": "MAXIMUS", "quantity": "3" }, { "product": "COLECCION", "quantity": "3" }, { "product": "CABERNET FRANC", "quantity": "6" }, { "product": "CHARDONNAY", "quantity": "6" }, { "product": "SAUVIGNON BLANC", "quantity": "6" } ] }; function makeGroup(data, max) { var selected = [], bottlesNum = 0; while(data.order.length) { if(bottlesNum + +data.order[0].quantity > max) break; var order = data.order.shift(); bottlesNum += +order.quantity; // casting to Number selected.push(order); } return selected; } function splitOrder(data, max) { while(data.order.length) { var results = makeGroup(data, max); if(!results.length) { console.log("Error: a product's quantity is greater than max. size of the group. Try to increase max. size of the group."); break; } console.log({ results: results }); } } // 2nd argument - max. size of the group. In case of 12 there will be 2 groups - of 3, 3, 6 and 6, 6 bottles splitOrder(data, 12); // Also notice that if max. size of the group is a changing value and can be set somehow to, lets say, 4 which is fewer than number of some products (6) in our order. So, it is impossible to complete such a task without taking some additional steps to handle this situation. For example, we could filter our data beforehand to exclude products with numbars greater than 4 and then form groups based on the rest of the data. Or we can treat products with number equal to 6 as if they satisfy our constraint etc. 

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

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