2017-05-15 15 views
0

を作成し、私はこの例のようにalasqlに戻るには、これらの値にループを作成する必要があります。私は、これらの値を持つ配列を持つforeachのJavaScriptでオブジェクト配列

http://jsfiddle.net/agershun/11gd86nx/5/

var data = { 
    "business": [ 
{ 
    "order_contents": [ 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 85, 
     "name": "product 3", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 84, 
     "name": "product 2", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 83, 
     "name": "product 1", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    }, 
    { 
     "id": 84, 
     "name": "product 2", 
     "price": "1.99", 
     "quantity": 1, 
     "total": "1.99", 
     "ingredients": [], 
     "extras": [] 
    } 
    ] 
    } 
] 
}; 

ここに私のコードです。 。

info.orderByChild("data").startAt("08/04/2017").endAt("12/06/2017").on('value', function(snapshot){ 
     snapshot.forEach(function(item) { 

      jsonObj = { 
       "business": [ 
      { 
       "order_contents": [ 
       { 
        "id": itemVal['id'], 
        "name": itemVal['name'], 
        "price": itemVal['price'], 
        "quantity": itemVal['quantity'], 
        "total": "itemVal['total'] 
       } 
       ] 
      } 
      ] 
     } 

そのない配列を作成し、最後の値だけ..

誰かが私を助けることができますか?

+0

は、そのようにタグ付けして下さい。 –

+0

ok、そのタグ付き!ありがとう – sealabr

+0

各反復で同じ変数( 'jsonObj')を上書きしているので、最後の項目で終わるのです。あなたは 'jsonObj'をどこで定義し、それを何のために使っていますか? –

答えて

1

あなたは初めに、あなたの配列を定義して、ループの中で後でそれに各オブジェクトを追加する必要があります。

var result = { business: [] }; // PLACEHOLDER 
var jsonObj; 
info.orderByChild("data").startAt("08/04/2017").endAt("12/06/2017").on('value', function(snapshot) { 
     snapshot.forEach(function(item) { 
      jsonObj = { 
       "order_contents": [ 
        { 
         "id": itemVal['id'], 
         "name": itemVal['name'], 
         "price": itemVal['price'], 
         "quantity": itemVal['quantity'], 
         "total": "itemVal['total'] 
        } 
       ] 
      }; 
      result.business.push(jsonObj); // ADDING EACH OBJECT TO YOUR ARRAY 
     }); 
    }); 
+0

しかし、その戻り値Array [オブジェクト、オブジェクト、オブジェクト......]は必要です..... Object {business:Array [1]} – sealabr

+1

ああ、私は分、答えを更新します... – errata

+1

待っていますが、あなたはすでにそのようなオブジェクトを持っていますか?あなたの 'data'変数はそのように構造化されています - ' Object {business:Array [1]} '... – errata

0

あなたは新しい配列にsnapshot内の項目をマッピングしたいので、代わりに使用しますforEach方法は、map方法を使用して得られた配列は、それが返されます。これはFirebase関連場合

jsonObj = snapshot.map(function(item) { 
    return { 
     "business": [ 
     { 
      "order_contents": [ 
      { 
       "id": item['id'], 
       "name": item['name'], 
       "price": item['price'], 
       "quantity": item['quantity'], 
       "total": item['total'] 
      } 
      ] 
     } 
     ] 
    } 
    }); 
関連する問題