最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create Object Array in foreach Javascript - Stack Overflow

programmeradmin3浏览0评论

I have an Array with these values, I need to create a loop on these values to return in alasql like this example:

/

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?

I have an Array with these values, I need to create a loop on these values to return in alasql like this example:

http://jsfiddle/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?

Share Improve this question edited May 15, 2017 at 14:29 Marc-Antoine 231 gold badge1 silver badge9 bronze badges asked May 15, 2017 at 14:26 sealabrsealabr 1,6924 gold badges21 silver badges31 bronze badges 5
  • If this is Firebase-related, please tag it as such. – user663031 Commented May 15, 2017 at 14:28
  • ok, its tagged! thanks – sealabr Commented May 15, 2017 at 14:29
  • You're overwriting the same variable (jsonObj) on each iteration, that's why it ends up with the last item. Where do you define jsonObj and what are you using it for? – Lennholm Commented May 15, 2017 at 14:29
  • i define as var jsonObj = []; and I use for alasql to create a 'database' of this object – sealabr Commented May 15, 2017 at 14:31
  • @sealabr Unfortunately your question has not aged well. jsFiddle sample is no longer available. It's unclear the return type of the startAt and endAt functions. By tailoring the payload and functions 'just enough to recreate the error questions will get faster responses and have greater longevity. – dank8 Commented Aug 22, 2023 at 0:31
Add a ment  | 

2 Answers 2

Reset to default 2

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:

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

评论列表(0)

  1. 暂无评论