简体   繁体   English

在foreach Javascript中创建对象数组

[英]Create Object Array in foreach Javascript

I have an Array with these values, I need to create a loop on these values to return in alasql like this example: 我有一个带有这些值的数组,我需要在这些值上创建一个循环以像下面的示例一样在alasql中返回:

http://jsfiddle.net/agershun/11gd86nx/5/ 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": []
    }
   ]
  }
 ]
};

here's my code.. 这是我的代码。

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

its not creating an array, only the last value.. 它不创建数组,仅创建最后一个值。

can someone help me? 有人能帮我吗?

You have to define your array in the beginning and add each object to it later on in your loop: 您必须在开头定义数组,然后在循环中向其添加每个对象:

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

You want to map the items in snapshot to a new array, so instead of using the forEach method, use the map method and the resulting array it returns: 您希望将snapshot的项目映射到新数组,因此,与其使用forEach方法, 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']
            }
          ]
        }
      ]
    }
  });

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

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