javaScript - 添加密钥&对象的价值

时间:2015-10-12 08:33:43

标签: javascript object

我的函数中有一个forEach用于创建对象:
请运行代码段:

angular.module('myApp', []).controller('myCntl', function($scope) {
  $scope.t = '';
   var input = "a,b,c,d,e,r \n1,1,1,1,1,1\n2,2,2,2,2,1 \n3,3,3,3,3,1";
  var rows = input.split('\n');
  var result = {
    header: [],
    body: []
  };

  //Get Header

  var headerString = rows[0].split(',');
  headerString.forEach(function(val) {
    result.header.push(val);
  });

  rows.splice(0, 1);
  rows.splice(rows.length - 1, rows.length); //delete "" row, from end array

  // Get Body 'a,b,c,d,...'
  rows.forEach(function(val, i) {
    var bodyString = val.split(',');
    var objBody = new Object;

    bodyString.forEach(function(val, i) {

      var strHeader = result.header[i];
      objBody[strHeader] = val;
    });

    result.body.push(objBody);
  });

  $scope.result = result.body;

  $scope.show = function() {
    console.log($scope.result)
    $scope.t = $scope.result;
  }



});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test" ng-app="myApp" ng-controller="myCntl">
  <button ng-click="show()">click me</button>
  <span ng-repeat="item in t">
   {{item}}
  </span>
</div>

而且,这是forEach之后的objBody:

objBody = {   
    a: "1",  
    b: "1",  
    "c": "1"  
}

现在,我的问题出现在 objBody 的最后一条记录中的键,双重qoutation
它是什么?为什么?! &GT; ( “C”)

enter image description here

2 个答案:

答案 0 :(得分:0)

问题是由于输入字符串中r\n之间的空白而引起的。按\n分割字符串时,rows[0]将为"a,b,c,d,e,r "。在用逗号分割之后,最后一个元素将包含像"r "这样的空格。

所以只需更改以下代码行

var input = "a,b,c,d,e,r \n1,1,1,1,1,1\n2,2,2,2,2,1 \n3,3,3,3,3,1";

var input = "a,b,c,d,e,r\n1,1,1,1,1,1\n2,2,2,2,2,1\n3,3,3,3,3,1";

解决问题。

angular.module('myApp', []).controller('myCntl', function($scope) {
  $scope.t = '';
   var input = "a,b,c,d,e,r \n1,1,1,1,1,1 \n2,2,2,2,2,1 \n3,3,3,3,3,1";
  input = input.replace(" ","");
  console.log(input);
  var rows = input.split('\n');
  var result = {
    header: [],
    body: []
  };

  //Get Header

  var headerString = rows[0].split(',');
  headerString.forEach(function(val) {
    result.header.push(val);
  });

  rows.splice(0, 1);
  rows.splice(rows.length - 1, rows.length); //delete "" row, from end array

  // Get Body 'a,b,c,d,...'
  rows.forEach(function(val, i) {
    var bodyString = val.split(',');
    var objBody = new Object;

    bodyString.forEach(function(val, i) {

      var strHeader = result.header[i];
      objBody[strHeader] = val;
    });

    result.body.push(objBody);
  });

  $scope.result = result.body;

  $scope.show = function() {
    console.log($scope.result)
    $scope.t = $scope.result;
  }



});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test" ng-app="myApp" ng-controller="myCntl">
  <button ng-click="show()">click me</button>
  <span ng-repeat="item in t">
   {{item}}
  </span>
</div>

编辑:您已找到答案。但是我从动态输入字符串中删除空格的方法是

input = input.replace(" ","");

答案 1 :(得分:-1)

我通过regexExp解决了split的问题:

var myRegex = new RegExp(/\s*\n/);
var rows = input.split(myRegex);

此命令拆分字符串中的每个'\ n'。这对我有用。