i have a array something like this ["\\n robort \\n \\ electronic","automated machine\\narm"];
how can i remove \\n from this array. so that my result will look like
[" robort electronic","automated machine arm"]
i'm able to remove seperate(or segregated) \\n with this code
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:
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.
User map()
( Link ) array and replace()
function.
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);
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.