简体   繁体   English

如何从与其他值聚合的jquery中的数组中删除\\ n

[英]how to remove \n from an array in jquery aggregated with other values

i have a array something like this ["\\n robort \\n \\ electronic","automated machine\\narm"]; 我有一个像这样的数组["\\n robort \\n \\ electronic","automated machine\\narm"];

how can i remove \\n from this array. 我如何从该数组中删除\\ n。 so that my result will look like 这样我的结果看起来像

[" robort electronic","automated machine arm"]

i'm able to remove seperate(or segregated) \\n with this code 我可以使用此代码删除单独的(或分隔的)\\ n

 var arr = ["\\n", "\\n roborts\\n"]; var removeItem = '\\n'; arr = jQuery.grep(arr, function(value) { return value != removeItem; }); console.log(arr); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 

You can map it back, and remove the newlines from each index, and filter out any that are just newlines 您可以将其映射回去,并从每个索引中删除换行符,并过滤掉所有只是换行符的内容

 var arr = ["\\n robort \\n \\ electronic","automated machine\\narm", "\\n"]; arr = arr.filter(function(item) { return item !== "\\n"; }).map(function(item) { return item.replace(/\\n/g,''); }); console.log(arr) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Without jQuery: 没有jQuery:

 var arr = ["\\n", "\\n roborts\\n"].map(function(val) { return val.replace(/\\n/g, ""); }); console.log(arr); 

This uses Array.prototype.map() to return an array consisting of the parts of your existing array but with the newline character removed. 这使用Array.prototype.map()返回一个包含现有数组各部分但删除了换行符的数组。

User map() ( Link ) array and replace() function. 用户map()Link )数组和replace()函数。

Code

var arr = ["\nautomated machine\narm", "\n roborts \n"];
var removeItem = /\n/g;

arr = arr.map( function(value) {
  return value.replace(removeItem, "");
});

console.log(arr);

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

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