2017-07-18 62 views
0

我想根据特定条件生成分组的JSON元素列表,但我无法使循环工作。 该功能应该与12个瓶子组合并返回一个JSON列表。所以在这个例子中,函数应该提取3个第一项,然后再次运行以提取剩余的项。但我永远循环...谢谢你在前进,在函数之间循环并存储结果

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 }; 

回答

0
for (i < 0; c = "true"; i++) 

奇怪的事情发生在这里。您不设置循环中的任何条件停止,只需将值“true”指定为c。尝试使用==而不是=;也初始化看起来很奇怪 - 设置i0。显然,它会使整个事情工作(至少循环将停止在某一点),但最终我得到变量results等于0。那里还有其他的错误/怪异的东西。 Propably,你想达到这样的事:

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

检查出来。

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.

+0

非常感谢您的弧线球,它的工作完美的第一个框,但我怎么能循环再获得剩余的2项?它应该生产2个盒子,其中一个包含前3个项目,第二个包含最后2个包装。 –

+0

啊......好的。让我添加几行,并立即检查更新的答案 – curveball

+0

@Deca F利马完成,检查更新的答案。它花了一点时间:) – curveball