-1

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.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?

5
  • If this is Firebase-related, please tag it as such. Commented May 15, 2017 at 14:28
  • ok, its tagged! thanks 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? Commented May 15, 2017 at 14:29
  • i define as var jsonObj = []; and I use for alasql to create a 'database' of this object 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. Commented Aug 22, 2023 at 0:31

2 Answers 2

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
        });
    });
Sign up to request clarification or add additional context in comments.

5 Comments

ok but its returns Array [ Object, Object, Object......] need to be .....Object { business: Array[1] }
Hm wait, but you have an object like that already? Your data variable is structured in that way - Object { business: Array[1] }...
yeah I need to create this object 'jsonObj' (in foreach) exactly like this 'data' ..because this 'data' is an example.
I updated my answer... Is that what you are looking for?
"never define variables inside a loop when possible" only pertains to regular for and while loops as they don't scope the variables properly. Defining variables in callbacks, as with forEach, is perfectly fine and definitely preferable to overwriting a variable in the outside closure.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.