Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I have a csv file with contents like this :
- playername,id,grelo,fidestd,fiderapid,fideblitz
- Jack Daniels,53365,0,0,0,0
- Super Caotonic,54257,1046,1617,1596,1626
- Amola kaloyumba,57854,0,0,0,0
- Hello world,0,0,0,0,0
- I want a function in javascript that will load this file once and create an array with it's contents.
- I want a second JS function named checkNameRelation(name) that will check if the string name contains any of the words (seperated by space) contained in playername or id of the csv. If yes then return a string in the follwing format : playername <a href="chessfed.gr/elo/id>id</a>gr:grelo |Fide s:fidestd,r:fiderapid,b:fideblitz
- ----------------------------
- Here is a sample code that achieves the requirements mentioned:
- First, we will create a function to load the CSV file and create an array with its contents:
- ```javascript
- let csvData = [];
- function loadCSV(file) {
- fetch(file)
- .then(response => response.text())
- .then(data => {
- csvData = data.split('\n').map(row => row.split(','));
- console.log(csvData);
- });
- }
- loadCSV('data.csv');
- ```
- Next, we will create the `checkNameRelation` function to check if the input `name` matches any playername or id in the CSV:
- ```javascript
- function checkNameRelation(name) {
- for (let i = 1; i < csvData.length; i++) {
- if (csvData[i][0].includes(name) || csvData[i][1].includes(name)) {
- return `${csvData[i][0]} <a href="chessfed.gr/elo/${csvData[i][1]}">${csvData[i][1]}</a> gr:${csvData[i][2]} | Fide s:${csvData[i][3]}, r:${csvData[i][4]}, b:${csvData[i][5]}`;
- }
- }
- return 'No match found.';
- }
- console.log(checkNameRelation('ΑΠΟΣΤΟΛΟΣ')); // Example usage
- ```
- Ensure that you replace `'data.csv'` with the correct path to your CSV file.
- You can now use the `checkNameRelation` function to check if a given name matches any playername or id in the CSV and get the formatted output as described.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement