简体   繁体   中英

javaScript - Add key & value to Object

There is a forEach in my function for create Object:
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:

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

Now, my problem is in key with double qoutation in last record of objBody .
What is it? and Why?! > ("c")

在此处输入图片说明

The problem was arising due to the white space between r and \\n in the input string. When you split the string by \\n , the rows[0] will be "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 " .

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:

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

This command split every ' \\n' in string. This work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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