简体   繁体   English

从数组中的元素修改空白元素

[英]Modifying White Space Elements From Element in Array

I have the following array of arrays: 我有以下数组数组:

arr1= ["avd befe, bla, bla, bla, bla", "cveve devev, bla, bla, bla, bla", "aava beve. cavbr, bla, bla, bla, bla", "feve gyhyh, bla, bla, bla, bla", "ibtbt, bla, bla, bla, bla"] 

In my example, I am focused on the first group of characters in the array. 在我的示例中,我专注于数组中的第一组字符。 These can include items that are of the format "x", "xy" (wa blank space between two groups of characters), and "xyz" (w two blank spaces between three groups of characters. 这些可以包括格式为“ x”,“ xy”(两个字符组之间的空白)和“ xyz”(三个字符组之间的两个空白)的项目。

I would like to delete the y from the third group of elements (so that "xyz" becomes "xz"). 我想从第三组元素中删除y(以使“ xyz”成为“ xz”)。 arr1 shown above is an example - the array I'm working with has a couple hundred entries. 上面显示的arr1是一个示例-我正在使用的数组有数百个条目。 In that example above, I'd want "aava beve. cavbr" to become "aava cavbr" in the third element in arr1. 在上面的示例中,我希望“ aava beve。cavbr”成为arr1中的第三个元素中的“ aava cavbr”。

I'm making assumptions, based on your question, that these will always and only be in the first place in the nested arrays. 我根据您的问题做出假设,这些假设将始终且仅在嵌套数组中排在首位。

// loop over the inner arrays
arr1.forEach(function(innerArr) {
  var parts = innerArr[0].split(' ');
  innerArr[0] = parts[0] + ' ' + parts[ parts.length - 1 ];
});

If those elements never have additional spaces in them (eg it's always "xyz" and not "xy some other stuff with spaces in it z" then you can just do innerArr[0].replace(/\\s[^ ]\\s/, ''); but I wasn't clear on the exact form of your data. 如果这些元素中再也没有空格(例如,它总是“ xyz”,而不是“ xy其他东西中有空格z”),则可以执行innerArr[0].replace(/\\s[^ ]\\s/, '');但是我不清楚您的数据的确切格式。

EDIT: 编辑:

Based on the new data format, here's more what you're after. 基于新的数据格式,这是您需要的更多内容。

var newArr = arr1.map(function(val) {
  var parts = val.split(',');
  var words = parts[0].split(' ');
  if (words.length > 2) {
    parts[0] = words[0] + ' ' + words[ words.length - 1 ];
  }
  return parts.join(',');
});

if i understand you question correctly , maybe code like this : 如果我正确理解您的问题,也许是这样的代码:

arr1.map(function(ele){
    var current = ele[0].split(' ');
    var head = current.shift();      // head is first group
    var tail = current.pop();        // tail is last  group
    // console.log(head.concat([' '],tail));
    return head.concat([' '],tail);
});

It looks to me like you want to get the first and last values and keep them separated by a whitespace character. 在我看来,您想要获取第一个和最后一个值,并用空格字符分隔它们。 Here's the easiest way I can think to do that: 这是我想到的最简单的方法:

var arr1 = ["avd befe, bla, bla, bla, bla", "cveve devev, bla, bla, bla, bla", "aava beve. cavbr, bla, bla, bla, bla", "feve gyhyh, bla, bla, bla, bla", "ibtbt, bla, bla, bla, bla"]

function clean(str) {
  var foo = str.split(",")[0];
  var bar = foo.substr(0, foo.indexOf(" ")) + foo.substr(foo.lastIndexOf(" "));
  return str.replace(foo, bar);
}
for (var i = 0; i < arr1.length; i++) {
    arr1[i] = clean(arr1[i]);
}

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

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