在foreach Javascript

时间:2017-05-15 14:26:10

标签: javascript

我有一个带有这些值的数组,我需要在这些值上创建一个循环,以便像这个例子一样在alasql中返回:

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']
                }
              ]
            }
          ]
        }

它没有创建数组,只有最后一个值..

有人可以帮助我吗?

2 个答案:

答案 0 :(得分: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
        });
    });

答案 1 :(得分:0)

您希望将snapshot中的项目映射到新数组,因此不使用forEach方法,而是使用map方法及其返回的结果数组:

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