How to create running totals in CouchDB (like SQL window functions) - javascript

Edited the question for clarity:
Assume I have the following documents in CouchDB:
{
"_id": "1",
"date": 1672119898,
"amount": 120,
"owner": "user_1"
},
{
"_id": "2",
"date": 1672119897,
"amount": 25,
"owner": "user_1"
},
{
"_id": "3",
"date": 1672119895,
"amount": 40,
"owner": "user_2"
},
{
"_id": "4",
"date": 1672119899,
"amount": 70,
"owner": "user_1"
},
{
"_id": "5",
"date": 1672119891,
"amount": 33,
"owner": "user_1"
},
{
"_id": "6",
"date": 1672119893,
"amount": 11,
"owner": "user_2"
}
And I'd like to create a view that returns the running total: ordered by ascending date, running total by owner.
Easy to do in SQL with window functions. But not sure how to achieve the same with CouchDB. I have no code to show.
Expected Output with running totals by owner, sorted by date descending:
[
{
"_id": "4",
"date": 1672119899,
"amount": 70,
"owner": "user_1",
"totalAmount": 248 // 178 + 70
},
{
"_id": "1",
"date": 1672119898,
"amount": 120,
"owner": "user_1",
"totalAmount": 178 // 120 + 58
},
{
"_id": "2",
"date": 1672119897,
"amount": 25,
"owner": "user_1",
"totalAmount": 58 // 25 + 33
},
{
"_id": "5",
"date": 1672119891,
"amount": 33,
"owner": "user_1",
"totalAmount": 33
},
{
"_id": "3",
"date": 1672119895,
"amount": 40,
"owner": "user_2",
"totalAmount": 51 // 40 + 51
},
{
"_id": "6",
"date": 1672119893,
"amount": 11,
"owner": "user_2",
"totalAmount": 11
}
]

Related

how to group by data in highcharts based on two categories?

I have created highchart in that i have to display data based on two group by condition but i am not able to group by based on that.
Here is my data, in this i have to group by based on countType and duration (x-axis:date ,y-axis:count), i have created fiddle as well.Please help.ANy help would be highly appreciated.
Fiddle: https://jsfiddle.net/p7tjm4r9/21/
[
{
"count": 40,
"countType": "DISCHARGE",
"date": "2021-06-11",
"duration": "ST-15"
},
{
"count": 27,
"countType": "CHARGE",
"date": "2021-06-11",
"duration": "ST-15"
},
{
"count": 23,
"countType": "CHARGE",
"date": "2021-06-12",
"duration": "ST-15"
},
{
"count": 23,
"countType": "CHARGE",
"date": "2021-06-14",
"duration": "ST-15"
},
{
"count": 40,
"countType": "DISCHARGE",
"date": "2021-06-13",
"duration": "ST-15"
},
{
"count": 1,
"countType": "DISCHARGEVOLTAGE",
"date": "2021-06-11",
"duration": "ST-15"
},
{
"count": 1,
"countType": "DISCHARGEVOLTAGE",
"date": "2021-06-13",
"duration": "ST-15"
},
{
"count": 40,
"countType": "DISCHARGE",
"date": "2021-06-11",
"duration": "EV1"
},
{
"count": 40,
"countType": "DISCHARGE",
"date": "2021-06-13",
"duration": "EV1"
},
{
"count": 1,
"countType": "DISCHARGE",
"dates": "2021-06-11",
"duration": "EV1"
},
{
"count": 6,
"countType": "CHARGE",
"date": "2021-06-11",
"duration": "EV1"
},
{
"count": 1,
"countType": "CHARGE",
"dates": "2021-06-12",
"duration": "EV1"
},
]

Looping through objects and creating array then combining on id

I have a problem trying to build an array, then combine and output multiple objects depending on the id.
This is the problem (I hope it makes sense):
I am looping through multiple objects, then create an array depending on the value of the type of the object. I then take that array and build it into the object. Once that is done I need to run through all the objects and combine any that have the same id.
this is an example of how it would originally look:
[{
"id": "755",
"entities": ["14394551"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 43,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"],
"exchangerate": 1
}, {
"id": "755",
"entities": ["14394553"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 28,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
"exchangerate": 1
},{
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 38,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14434400"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 18,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14430895"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 63,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "Dep",
"salesOrderId": ["14430894"],
"memo": ["MICGB2452"],
"exchangerate": 1
}, {
"id": "762",
"entities": ["14350538"],
"accountID": "762",
"accountName": "122100 TEST1",
"amount": 45,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
"exchangerate": 1
}, {
"id": "760",
"entities": ["14350538"],
"accountID": "760",
"accountName": "122100 TEST2",
"amount": 49,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"],
"exchangerate": 1
}]
then this is after the array has been created -> Depending on the type.
[
{
"id": "755",
"accountID": "755",
"accountName": "122060 Test7",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14394551"],
"amount": 43,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"]
}],
"deposit": []
}, {
"id": "755",
"accountID": "755",
"accountName": "122060 Test7",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14394553"],
"amount": 28,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
}],
"deposit": []
},{
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14439896"],
"amount": 38,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"]
}],
"deposit": []
}, {
"id": "758",
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit" : [{
"entities": ["14434400"],
"amount": 18,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"]
}],
"deposit": []
}, {
"id": "758",
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "Dep",
"exchangerate": 1,
"customerDeposit": [],
"deposit": [{
"entities": ["14430895"],
"amount": 63,
"memo": ["MICGB2452"],
}]
}, {
"id": "762",
"accountID": "762",
"accountName": "122100 TEST1",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [
{
"entities": ["14350538"],
"amount": 45,
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
}
],
"deposit": []
}, {
"id": "760",
"accountID": "760",
"accountName": "122100 TEST2",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14350538"],
"amount": 49,
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"]
}],
"deposit": []
}]
then this would be what I want the final output to be - all the same id objects have the customerDeposit / deposit combined.
[
{
"id": "755",
"accountID": "755",
"accountName": "122060 Test7",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14394551"],
"amount": 43,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"]
}, {
"entities": ["14394553"],
"amount": 28,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
}],
"deposit": []
}, {
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14439896"],
"amount": 38,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"]
}, {
"entities": ["14434400"],
"amount": 18,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"]
}],
"deposit": [{
"entities": ["14430895"],
"amount": 63,
"memo": ["MICGB2452"],
}]
}, {
"id": "762",
"accountID": "762",
"accountName": "122100 TEST1",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [
{
"entities": ["14350538"],
"amount": 45,
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
}
],
"deposit": []
}, {
"id": "760",
"accountID": "760",
"accountName": "122100 TEST2",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14350538"],
"amount": 49,
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"]
}],
"deposit": []
}]
This is my current code but it is not working as i want as it skips any ids that dont have more than one customerDeposit / deposit as it doesn't get into the if (objForId) statement.
function createDeposit(values) {
deposit = {
entities: values.entities,
amount: values.amount,
memo: values.memo,
};
return deposit;
}
function createCustomerDeposit(values) {
customerDeposit = {
entities: values.entities,
amount: values.amount,
memo: values.memo,
webPaymentMethod: values.webPaymentMethod,
webPaymentMethodID: values.webPaymentMethodID,
salesOrderId: values.salesOrderId,
};
return customerDeposit;
}
function run() {
const transferData = [{
"id": "755",
"entities": ["14394551"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 43,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"],
"exchangerate": 1
}, {
"id": "755",
"entities": ["14394553"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 28,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
"exchangerate": 1
},{
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 38,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14434400"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 18,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14430895"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 63,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "Dep",
"salesOrderId": ["14430894"],
"memo": ["MICGB2452"],
"exchangerate": 1
}, {
"id": "762",
"entities": ["14350538"],
"accountID": "762",
"accountName": "122100 TEST1",
"amount": 45,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
"exchangerate": 1
}, {
"id": "760",
"entities": ["14350538"],
"accountID": "760",
"accountName": "122100 TEST2",
"amount": 49,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"],
"exchangerate": 1
}];
var customerDeposit = [];
var deposit = [];
var res = transferData.reduce(function (agg, obj) {
var objForId = agg.filter(function (idObj) {
return idObj.id === obj.id;
})[0];
if (objForId) {
if(obj.type === 'CustDep') {
objForId.customerDeposit.push(createCustomerDeposit(obj));
}
if(obj.type === 'Dep') {
objForId.deposit.push(createDeposit(obj));
}
} else {
agg.push({
id: obj.id,
accountID: obj.accountID,
accountName: obj.accountName,
subsidiary: obj.subsidiary,
currency: obj.currency,
exchangerate: obj.exchangerate,
customerDeposit,
deposit
});
customerDeposit = [];
deposit = [];
}
return agg;
}, []);
console.log(res);
}
run();
Any help would be really appreciated. Hopefully the question makes sense.
Thanks

Count dublicates in array of objects [duplicate]

This question already has answers here:
Group objects by multiple properties in array then sum up their values
(16 answers)
Closed 1 year ago.
I have an array. How can I find dublicates by type, name and size and increment amount.
And after increasing amount delete the same ones.
[
{
"name": "Pizza with pepper",
"imageUrl": "...",
"type": 0,
"size": 26,
"price": "803",
"amount": 1
},
{
"name": "Pizza with pepper",
"imageUrl": "...",
"type": 1,
"size": 40,
"price": "803",
"amount": 1
},
{
"name": "Peperoni",
"imageUrl": "...",
"type": 0,
"size": 30,
"price": "803",
"amount": 1
},
{
"name": "Peperoni",
"imageUrl": "...",
"type": 0,
"size": 30,
"price": "803",
"amount": 1
}
]
const data = [
{
"name": "Pizza with pepper",
"imageUrl": "...",
"type": 0,
"size": 26,
"price": "803",
"amount": 1
},
{
"name": "Pizza with pepper",
"imageUrl": "...",
"type": 1,
"size": 40,
"price": "803",
"amount": 1
},
{
"name": "Peperoni",
"imageUrl": "...",
"type": 0,
"size": 30,
"price": "803",
"amount": 1
},
{
"name": "Peperoni",
"imageUrl": "...",
"type": 0,
"size": 30,
"price": "803",
"amount": 1
}
];
const merged = data.reduce((agg, item) => {
const key = `${item.name}-${item.type}-${item.size}`;
if (agg[key]) {
agg[key].amount += item.amount;
} else {
agg[key] = item;
}
return agg;
}, {});
const result = Object.values(merged);
console.log(result);

Combine items in array based on property and maintain reference

I have an array of items that I am creating a timeline out of.
If 2 or more items are close together on the x axis (using the 'x' value') I want to combine them.
Loop through the array and compare the 'x' property. If they are close together then push the item into the 'instances' field of both items.
Change the date of both items to use the date of the first item.
Keep the original date in a field called 'idDate'
If an item is considered close to another item, make sure that item hasn't already been combined. If it has, use the 'x' property of the item it was combined with.
In the code I created an array called 'desiredArray' to show what I want me output to be
My current code pushes close items into the 'instances' array of the first close item but does not maintain the other items in the array. I also can't figure out how to do the date part. Any help is appreciated.
const array = [{
"date": "2017-03-04T13:30:00Z",
"id": "7",
"x": "-448.056888554414",
"instances": [{
"date": "2017-03-04T13:30:00Z",
"id": "7",
"x": "-448.056888554414"
}]
},
{
"date": "2017-08-13T13:30:00Z",
"id": "11",
"x": "25.521193637366817",
"instances": [{
"date": "2017-08-13T13:30:00Z",
"id": "11",
"x": "25.521193637366817"
}]
},
{
"date": "2017-08-15T13:30:00Z",
"id": "12",
"x": "31.296536103120246",
"instances": [{
"date": "2017-08-15T13:30:00Z",
"id": "12",
"x": "31.296536103120246"
}]
},
{
"date": "2017-08-20T13:30:00Z",
"id": "13",
"x": "39.95954980175038",
"instances": [{
"date": "2017-08-20T13:30:00Z",
"id": "13",
"x": "39.95954980175038"
}]
}
];
const newArray = [];
newArray.push({
"date": "2018-08-06T13:30:00Z",
"id": "14",
"x": "639.95954980175038",
"instances": [{
"date": "2018-08-06T13:30:00Z",
"id": "14",
"x": "1054"
}]
});
array.reverse().forEach((current: any) => {
const last = newArray[newArray.length - 1];
if (last.x - current.x < 40) {
last.instances.push(current.instances[0]);
} else {
newArray.push(current);
}
});
console.log(newArray);
// this is what I'm trying to create
const desiredArray = [{
"date": "2018-08-06T13:30:00Z",
"id": "14",
"x": "639.95954980175038",
"instances": [{
"date": "2018-08-06T13:30:00Z",
"id": "14",
"x": "1054"
}]
},
{
"date": "2017-08-20T13:30:00Z",
"idDate": "2017-08-20T13:30:00Z",
"id": "13",
"x": "39.95954980175038",
"instances": [{
"date": "2017-08-13T13:30:00Z",
"id": "11",
"x": "25.521193637366817"
},
{
"date": "2017-08-15T13:30:00Z",
"id": "12",
"x": "31.296536103120246"
},
{
"date": "2017-08-20T13:30:00Z",
"id": "13",
"x": "39.95954980175038"
}
]
},
{
"date": "2017-08-20T13:30:00Z",
"idDate": "2017-08-15T13:30:00Z",
"id": "12",
"x": "31.296536103120246",
"instances": [{
"date": "2017-08-13T13:30:00Z",
"id": "11",
"x": "25.521193637366817"
},
{
"date": "2017-08-15T13:30:00Z",
"id": "12",
"x": "31.296536103120246"
},
{
"date": "2017-08-20T13:30:00Z",
"id": "13",
"x": "39.95954980175038"
}
]
},
{
"date": "2017-08-20T13:30:00Z",
"idDate": "2017-08-13T13:30:00Z",
"id": "11",
"x": "25.521193637366817",
"instances": [{
"date": "2017-08-13T13:30:00Z",
"id": "11",
"x": "25.521193637366817"
},
{
"date": "2017-08-15T13:30:00Z",
"id": "12",
"x": "31.296536103120246"
},
{
"date": "2017-08-20T13:30:00Z",
"id": "13",
"x": "39.95954980175038"
}
]
},
{
"date": "2017-03-04T13:30:00Z",
"idDate": "2017-03-04T13:30:00Z",
"id": "7",
"x": "-448.056888554414",
"instances": [{
"date": "2017-03-04T13:30:00Z",
"id": "7",
"x": "448.056888554414"
}]
}
]

Loop outputs [object, object, object object] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have been taking some online courses recently to try and understand the basics of programming, Gradually trying to increase the complexity of what I am learning. However I cannot seem to be able to control the output of my loop, I either get the last value or [object,object object,object object, object,object]
Any help would be greatly appreciated, I am sure this is quite simple but I have tried for in's for's and for each's and no luck so far.
{
"years": [
{
"id": "1",
"year": "2015",
"total": "55045",
"points": [
{
"id": "2",
"points": "600",
"total": "215",
"percent": "0.4"
},
{
"id": "3",
"points": "500-599",
"total": "5431",
"percent": "9.9"
},
{
"id": "4",
"points": "400-499",
"total": "14097",
"percent": "25.6"
},
{
"id": "5",
"points": "300-399",
"total": "14446",
"percent": "26.2"
},
{
"id": "6",
"points": "200-299",
"total": "9768",
"percent": "17.7"
},
{
"id": "7",
"points": "100-199",
"total": "6562",
"percent": "11.9"
},
{
"id": "8",
"points": " >100",
"total": "4526",
"percent": "8.2"
}
]
},
{
"id": "9",
"year": "2014",
"total": "54025",
"points": [
{
"id": "10",
"points": "600",
"total": "162",
"percent": "0.3"
},
{
"id": "11",
"points": "500-599",
"total": "5088",
"percent": "9.4"
},
{
"id": "12",
"points": "400-499",
"total": "13447",
"percent": "24.9"
},
{
"id": "13",
"points": "300-399",
"total": "14047",
"percent": "26"
},
{
"id": "14",
"points": "200-299",
"total": "9584",
"percent": "17.7"
},
{
"id": "15",
"points": "100-199",
"total": "6926",
"percent": "12.8"
},
{
"id": "16",
"points": " >100",
"total": "4771",
"percent": "8.8"
}
]
},
{
"id": "17",
"year": "2013",
"total": "52767",
"points": [
{
"id": "18",
"points": "600",
"total": "152",
"percent": "0.3"
},
{
"id": "19",
"points": "500-599",
"total": "4813",
"percent": "9.1"
},
{
"id": "20",
"points": "400-499",
"total": "12803",
"percent": "24.3"
},
{
"id": "21",
"points": "300-399",
"total": "13381",
"percent": "25.4"
},
{
"id": "22",
"points": "200-299",
"total": "9566",
"percent": "18.1"
},
{
"id": "23",
"points": "100-199",
"total": "6914",
"percent": "13.1"
},
{
"id": "24",
"points": " >100",
"total": "5138",
"percent": "9.7"
}
]
},
{
"id": "25",
"year": "2012",
"total": "52589",
"points": [
{
"id": "26",
"points": "600",
"total": "165",
"percent": "0.2"
},
{
"id": "27",
"points": "500-599",
"total": "5026",
"percent": "9.6"
},
{
"id": "28",
"points": "400-499",
"total": "12395",
"percent": "23.6"
},
{
"id": "29",
"points": "300-399",
"total": "13170",
"percent": "25"
},
{
"id": "30",
"points": "200-299",
"total": "9588",
"percent": "18.2"
},
{
"id": "31",
"points": "100-199",
"total": "6999",
"percent": "13.3"
},
{
"id": "32",
"points": " >100",
"total": "5276",
"percent": "10"
}
]
},
{
"id": "33",
"year": "2011",
"total": "54341",
"points": [
{
"id": "34",
"points": "600",
"total": "162",
"percent": "0.3"
},
{
"id": "35",
"points": "500-599",
"total": "4863",
"percent": "8.6"
},
{
"id": "36",
"points": "400-499",
"total": "12235",
"percent": "22.5"
},
{
"id": "37",
"points": "300-399",
"total": "13860",
"percent": "18.4"
},
{
"id": "38",
"points": "200-299",
"total": "9966",
"percent": "18.4"
},
{
"id": "39",
"points": "100-199",
"total": "7477",
"percent": "13.8"
},
{
"id": "40",
"points": " >100",
"total": "5928",
"percent": "10.9"
}
]
},
{
"id": "34",
"year": "2010",
"total": "54480",
"points": [
{
"id": "35",
"points": "600",
"total": "136",
"percent": "0.2"
},
{
"id": "36",
"points": "500-599",
"total": "4564",
"percent": "8.4"
},
{
"id": "37",
"points": "400-499",
"total": "11973",
"percent": "22"
},
{
"id": "38",
"points": "300-399",
"total": "13878",
"percent": "25.5"
},
{
"id": "39",
"points": "200-299",
"total": "10391",
"percent": "19.1"
},
{
"id": "40",
"points": "100-199",
"total": "7294",
"percent": "13.4"
},
{
"id": "41",
"points": " >100",
"total": "6244",
"percent": "11.5"
}
]
}
]
}
I am hoping someone can maybe help with controlling the output of the loop.
You haven't showed any code, but I can guess that your problem is something typical in JavaScript:
JavaScript closure inside loops – simple practical example
Check out neurosnap's answer.
http://coffeescript.org/#loops
In CoffeeScript it's even simpler:
for filename in list
do (filename) ->
fs.readFile filename, (err, contents) ->
compile filename, contents.toString()
Basically, you need a lambda/IIFE inside the for loop in JavaScript to loop to avoid your problem.

Categories

Resources