So I am working on a project and am having trouble trying to use multiple arrays of data in a data table.
The issue I am running into is my initial set of data comes from a sharepoint list call(the calculations variable).
I then use values in that array to run some calculations and put those into their own array(formulatedNumbers).
I have my DataTable filling the first 6 columns with data fine(with the calculations array), the issue is getting the second array(formulatedNumbers) into the 7th column.
My javascript buiding the datatable is below
TL;DR After I use the first array to fill in columns 1-6, I want to use another array to fill in column 7.
JavaScript
x
26
26
1
function displayCalculations() {
2
$("#table_id").dataTable().fnDestroy();
3
var calculations = getRateData();
4
var formulatedNumbers = [];
5
6
for(i=0; i<calculations.length; i++) {
7
formulatedNumbers.push(calculations[i].annum * calculations[i].dailyDecimal * 1000);
8
}
9
10
console.log(formulatedNumbers);
11
12
$('#table_id').DataTable(
13
{
14
data: calculations,
15
"columns":
16
[
17
{ "data": "startDate" },
18
{ "data": "endDate" },
19
{ "data": "dayTotal" },
20
{ "data": "annum" },
21
{ "data": "dailyRate" },
22
{ "data": "dailyDecimal" }
23
],
24
});
25
}
26
Advertisement
Answer
I suggest just adding the new attribute to each item in calculations
.
JavaScript
1
25
25
1
function displayCalculations() {
2
$("#table_id").dataTable().fnDestroy();
3
var calculations = getRateData();
4
5
calculations.forEach(item =>
6
item.newField = item.annum * item.dailyDecimal * 1000);
7
8
console.log(calculations);
9
10
$('#table_id').DataTable(
11
{
12
data: calculations,
13
"columns":
14
[
15
{ "data": "startDate" },
16
{ "data": "endDate" },
17
{ "data": "dayTotal" },
18
{ "data": "annum" },
19
{ "data": "dailyRate" },
20
{ "data": "dailyDecimal" },
21
{ "data": "newField" } // + ADDED
22
],
23
});
24
}
25