简体   繁体   English

javaScript - 将键和值添加到对象

[英]javaScript - Add key & value to Object

There is a forEach in my function for create Object:我的函数中有一个 forEach 用于创建对象:
Please Run code snippet:请运行代码片段:

 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>

And, this is objBody after forEach:而且,这是 forEach 之后的 objBody:

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

Now, my problem is in key with double qoutation in last record of objBody .现在,我的问题是objBody的最后一个记录双qoutation关键
What is it?它是什么? and Why?!为什么?! > ("c") > ("c")

在此处输入图片说明

The problem was arising due to the white space between r and \\n in the input string.问题是由于输入字符串中r\\n之间的空格引起的。 When you split the string by \\n , the rows[0] will be "a,b,c,d,e,r " .当你用\\n分割字符串时, rows[0]将是"a,b,c,d,e,r " And after you split it with comma, then the last element will contain the white space like this "r " .用逗号分割后,最后一个元素将包含像这样的空格"r "

So just change the following line of code所以只需更改以下代码行

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";

to

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";

to fix the issue.解决问题。

 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>

EDIT: You have found some answer.编辑:你已经找到了一些答案。 But my way for removing white spaces from the dynamic input string would be但是我从动态输入字符串中删除空格的方法是

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

I solved problem with split by regexExp:我通过 regexExp 解决了拆分问题:

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

This command split every ' \\n' in string.此命令拆分字符串中的每个 '\\n'。 This work for me.这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM