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?
Marc-Antoine
231 gold badge1 silver badge9 bronze badges
asked May 15, 2017 at 14:26
sealabr
1,7124 gold badges21 silver badges31 bronze badges
2 Answers 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
});
});
answered May 15, 2017 at 14:31
errata
6,07110 gold badges59 silver badges103 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
sealabr
ok but its returns Array [ Object, Object, Object......] need to be .....Object { business: Array[1] }
errata
Hm wait, but you have an object like that already? Your
data variable is structured in that way - Object { business: Array[1] }...sealabr
yeah I need to create this object 'jsonObj' (in foreach) exactly like this 'data' ..because this 'data' is an example.
errata
I updated my answer... Is that what you are looking for?
Lennholm
"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.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']
}
]
}
]
}
});
answered May 15, 2017 at 14:40
Lennholm
7,5301 gold badge23 silver badges32 bronze badges
Comments
lang-js
jsonObj) on each iteration, that's why it ends up with the last item. Where do you definejsonObjand what are you using it for?