最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to search a CSV file using a key in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I want to create a function in JavaScript that searches a CSV file using a someones name and then outputs the corresponding email address

CSV File Format:

 Name:               Email:
exampleName      [email protected]
exampleName2     [email protected]
exampleName3     [email protected]

I think I might be able to use PapaParse as the file will be stored online in a remote directory (not a local file).

Example Of PapaParse /

    // Parse CSV string
var data = Papa.parse(csv);

// Convert back to CSV
var csv = Papa.unparse(data);

// Parse local CSV file
Papa.parse(file, {
    plete: function(results) {
        console.log("Finished:", results.data);
    }
});

// Stream big file in worker thread
Papa.parse(bigFile, {
    worker: true,
    step: function(results) {
        console.log("Row:", results.data);
    }
});

If someone could help me create this function it would be much appreciated. Thank you in advanced.

I want to create a function in JavaScript that searches a CSV file using a someones name and then outputs the corresponding email address

CSV File Format:

 Name:               Email:
exampleName      [email protected]
exampleName2     [email protected]
exampleName3     [email protected]

I think I might be able to use PapaParse as the file will be stored online in a remote directory (not a local file).

Example Of PapaParse https://www.papaparse./

    // Parse CSV string
var data = Papa.parse(csv);

// Convert back to CSV
var csv = Papa.unparse(data);

// Parse local CSV file
Papa.parse(file, {
    plete: function(results) {
        console.log("Finished:", results.data);
    }
});

// Stream big file in worker thread
Papa.parse(bigFile, {
    worker: true,
    step: function(results) {
        console.log("Row:", results.data);
    }
});

If someone could help me create this function it would be much appreciated. Thank you in advanced.

Share Improve this question asked Jul 6, 2018 at 14:50 L HethL Heth 131 gold badge1 silver badge6 bronze badges 4
  • use cdnjs.cloudflare./ajax/libs/jquery-csv/0.8.9/jquery.csv.js – Ullas Hunka Commented Jul 6, 2018 at 14:52
  • @UllasHunka That doesn't really answer my question – L Heth Commented Jul 6, 2018 at 14:56
  • That was not an answer but a suggestion – Ullas Hunka Commented Jul 6, 2018 at 14:57
  • stackoverflow./questions/14446447/… this is an example of doing it. – Ullas Hunka Commented Jul 6, 2018 at 15:00
Add a ment  | 

1 Answer 1

Reset to default 4

const csv = "Name,Email\nexampleName,[email protected]\nexampleName2,[email protected]"
const csvData = Papa.parse(csv, {header:true}).data

function findEmailByName(name) {
  return csvData.filter(data => data.Name === name)[0].Email
}

console.log(
  findEmailByName('exampleName2')
);
<script src="https://cdnjs.cloudflare./ajax/libs/PapaParse/4.5.0/papaparse.min.js">
</script>

发布评论

评论列表(0)

  1. 暂无评论