使用split来分离数组中的元素,JavaScript

时间:2018-03-13 22:36:39

标签: javascript arrays split

我正在寻找创建一个表格,该表格将包含3个以逗号分隔的项目,并将它们插入到表格中。目前我的表失败,或者将所有项目组合在一起。在每个标题下,我有一行,每行充满“马德里,西班牙,3255944”,“圣地亚哥,智利,4837295”,“利马,秘鲁,7737002”,在国家,首都,人口的三个标题下,我想分3行每个标题。

<html>
<head>
<title>  </title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>

<table id="table" border="1">

<!-- The Row Number 0 -->
<tr>
<th>City</th>
<th>Country</th>
<th>Population</th>
</tr>

</table>

<script>

var array = [
["Madrid,Spain,3255944", "Santiago,Chile,4837295",
"Lima,Peru,7737002"],
        ],

var a = array.split(',')
a[0], a[1], a[2];
table = document.getElementById("table");



for(var i = 0; i < array.length; i++)
{
// create a new row
var newRow = table.insertRow(table.length);
for(var j = 0; j < array[i].length; j++)
{
// create a new cell
var cell = newRow.insertCell(j);

// add value to the cell
cell.innerHTML = array[i][j];


}
}
</script>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

您需要在数组中的字符串上调用app.directive('fileUpload', function ($parse) { return { scope: { fileUpload: '=', callback: '&' }, link: function (scope, el, attrs) { el.bind('change', function (event) { var file = event.target.files[0]; scope.callback({ file }); }); } }; }); app.controller('main', ($scope) => { $scope.files1 = []; $scope.files2 = []; $scope.functionA = (file) => { $scope.files1.push(file); } $scope.functionB = (file) => { $scope.files2.push(file); } }); ,而不是数组本身。

  <div ng-controller="main">
    <input type="file" file-upload callback="functionA(file)" id="file1">
    <input type="file" file-upload callback="functionB(file)" id="file2">
  </div>

答案 1 :(得分:0)

巴马的答案当然是正确的,但除了我的肯定,我确实很感激。另一个来源提供了以下更容易掌握的,对我来说初学者自我。下面更多的是使用像我这样的学习者,一旦完成任务,我将纠正它并提供完整的代码,非常感谢绅士花时间,我将在时间允许时学习我的语法。

            var array = [
        "Madrid,Spain,3255944", "Santiago,Chile,4837295", "Lima,Peru,7737002"
    ];
    table = document.getElementById("table");

var x;
for (var i = 0; i < array.length; i++){

    <tr>
        x = arr[i];
    var pieces = array.split(',');
    <td> pieces[0] </td> 
    <td> pieces[1] </td>
    <td> pieces[2] </td>
</tr>;

        }