简体   繁体   English

从数组中的javascript对象的属性中删除特殊字符

[英]Remove special character from property of a javascript object in an array

I have a simple javascript array of objects like this:我有一个简单的 javascript 对象数组,如下所示:

  $scope.dataList = [];
                var obj = { "SLicenseNo": "NM-2136", "SBusinessnameLegal": "Luxury Auto Mall of Sioux Falls, Inc.", "SNameFirstSoleproprietor": "Edward"};
                $scope.dataList.push(obj);

My dataList array has hundreds of such objects.我的 dataList 数组有数百个这样的对象。 I want to remove the comma from the SBusinessnameLegal property or any other property that may have a comma.我想从 SBusinessnameLegal 属性或任何其他可能有逗号的属性中删除逗号。 I would like to replace it by a space我想用空格代替

So my end object would be something like所以我的最终对象会是这样的

 obj = { "SLicenseNo": "NM-2136", "SBusinessnameLegal": "Luxury Auto Mall of Sioux Falls Inc.", "SNameFirstSoleproprietor": "Edward"};

I have tried converting the array to a json string then .replace(',', ' ');我尝试将数组转换为 json 字符串然后 .replace(',', ' ');

But that isnt working.但这不起作用。 Any ideas or pointers on how I can pass an array to a function and it would strip a particular character from all properties of objects in the array ?关于如何将数组传递给函数并从数组中对象的所有属性中去除特定字符的任何想法或指针? TIA TIA

Try any of these scenarios :尝试以下任一场景:

  • for single object having multiple properties with comma (,)对于具有多个属性的单个对象,逗号(,)

 var obj = { "SLicenseNo": "NM-2136", "SBusinessnameLegal": "Luxury Auto Mall of Sioux Falls, Inc.", "SNameFirstSoleproprietor": "Edward" }; var newObj = {}; for (var i in obj) { newObj[i] = obj[i].replace(',',''); } console.log(newObj);

  • For array of objects try this.对于array of objects试试这个。

 var dataList = [ { "SLicenseNo": "NM-2136", "SBusinessnameLegal": "Luxury Auto Mall of Sioux Falls, Inc.", "SNameFirstSoleproprietor": "Edward" },{ "SLicenseNo": "NM-2136, Test", "SBusinessnameLegal": "Luxury Auto, Mall of Sioux Falls Inc.", "SNameFirstSoleproprietor": "Edward" }]; dataList.map(function(item) { var keyse = Object.keys(item); for (var i in keyse) { item[keyse[i]] = item[keyse[i]].replace(',',''); } }); console.log(dataList);

try this尝试这个

Object.keys(obj).forEach( function(key){ obj[key] = obj[key].replace(/,/g, " ") } );

If you have this in an array then如果你把它放在一个数组中,那么

$scope.dataList = $scope.dataList.map( function( obj ){
  Object.keys(obj).each( function(key){ obj[key] = obj[key].replace(/,/g, " ") } );
  return obj;
});

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

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