2017-07-18 8 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++) 

、事前にありがとうございます。サイクルの停止条件を設定していない場合は、cに値 "true"を割り当てます。 =の代わりに==を使用してください。初期化も奇妙に見えます - i0に設定してください。どうやら、それはすべてのものが動作するようになります(少なくともループはある時点で停止します)が、最終的には変数results0に等しいことがわかります。そこに他の間違い/奇妙なものがあります。

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

それをチェックアウト:Propably、あなたはこのような何かを達成したかったです。

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つのボックス、1つは3つのアイテム、2つは最後の2つのアイテムとなるはずです。 –

+0

ああ... ok。私はいくつかの行を追加し、瞬時に更新された答えを確認してみましょう – curveball

+0

@デカFリマは、更新された答えを確認します。それは少し以上の時間がかかりました:) – curveball

関連する問題