0

First of all, I am a .Net developer and I don't have much knowledge about JavaScript.

I have given to build a system using JavaScript which is working 100% offline. I have a big data set like below as CSV file.

+------+---------+----+----+----+----+-------+----------+
| Year | Subject | A  | B  | C  | F  | Total | PassRate |
+------+---------+----+----+----+----+-------+----------+
| 2015 | Maths   | 12 | 20 | 10 |  5 |    47 |       80 |
| 2015 | Sinhala | 18 | 14 |  5 | 10 |    47 |       75 |
| 2016 | Maths   | 25 | 15 |  4 |  8 |    52 |       25 |
| 2016 | Sinhala | 20 | 12 |  2 | 18 |    52 |       60 |
+------+---------+----+----+----+----+-------+----------+

This is just a sample, there are around 50 rows.

I tried to read CSV and tried json,xml files, But I didn't have a success because of cross origin policy

So I thought to save all these data in Multi Dimension array or similar thing. I read How can I create a two dimensional array in JavaScript? but no luck.

So, how can I save all these data to JavaScript variable?

Please note that I have to do lot of calculations (eg: total pass rate of each year) from above table, so I need to access any value easily.

So how can I save all these data to array or similar one?

Note : I am not asking that how to convert CSV to JavaScript variable.I am just asking that how can I save these type of lot of data as JS varible array or similar which allow to do calculation easily.

  • Sure, use an array, an array of objects, or an object of arrays, or whatever is most convenient for whatever you're trying to do with the data... – CertainPerformance Aug 07 '18 at 04:26
  • @CertainPerformance I read entire article (https://www.w3schools.com/js/js_arrays.asp) But I have no idea how can I create such type of array using JavaScript. I know how to create similar one using PHP.... Can you please give me a link – I am the Most Stupid Person Aug 07 '18 at 04:28

1 Answers1

1

You can specify it as arrays inside an array like below:

var firstArray = [];
firstArray.push(['Year', 'Subject', 'A', 'B', 'C', 'F', 'Total', 'PassRate']); // headers
firstArray.push([2015, 'Maths', 12, 20, 10, 5, 47, 80]); // 1st row
firstArray.push([2015, 'Sinhala', 18, 14, 5, 10, 47, 75]) // 2nd row
console.log(firstArray);

or as objects inside an array like below:

var secondArray = [];
secondArray.push({
  'Year': 2015,
  'Subject': 'Maths',
  'A': 12,
  'B': 20,
  'C': 10,
  'F': 5,
  'Total': 47,
  'PassRate': 80
}); // 1st row
secondArray.push({
  'Year': 2015,
  'Subject': 'Sinhala',
  'A': 18,
  'B': 14,
  'C': 5,
  'F': 10,
  'Total': 47,
  'PassRate': 75
}); // 2nd row
console.log(secondArray)
riyas
  • 71
  • 5
  • Thanks Riyas. First example working fine. Let say If I want to find the answer for "A" of 2015 Maths, I need to read `firstArray[1][3]`, Instead of that, is there any where to build the array to read it is like `firstArray[2015]["Maths"]`? – I am the Most Stupid Person Aug 08 '18 at 06:38
  • @IamtheMostStupidPerson I don't think the array can be modified to access it like `firstArray[2015]["Maths"]`. If you want to access elements like `firstArray[2015]["Maths"]`, you need to change the data structure to Object (instead of Array). – riyas Aug 08 '18 at 07:05