使用 javascript 将数据从文本文件导入数组

Importing data from a text file into an array using javascript

本文关键字:文件 导入 数组 文本 javascript 数据 使用      更新时间:2023-09-26

我想在数组中导入这些数据文件值。所以 times[0] 将是 23,8 等...

示例数据文件 web.txt(温度值以 C° 为单位(:

23,8
23,2
22,8
22,0
... etc

下面的代码是我迄今为止在网络上阅读的代码.txt文件,并在浏览器中显示值。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>load demo</title>
  <style>
  body {
    font-size: 16px;
    font-family: Arial;
  }
  </style>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p id="myid"></p>
<script>
var times = [];
 $.get('web.txt', function(data) {
        //var fileDom = $(data);
        var lines = data.split("'n");
        $.each(lines, function(n, elem) {
            $('#myid').append('<div>' + elem + '</div>');
        });
    });

</script>
</html>

这行得通。

只需重命名

var lines = data.split("'n");
$.each(lines, function(n, elem) ...

times = data.split("'n");
$.each(times, function(n, elem) ...