首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组,独立地修改数组中的拆分词

数组,独立地修改数组中的拆分词
EN

Stack Overflow用户
提问于 2018-04-29 08:29:22
回答 2查看 45关注 0票数 0

代码语言:javascript
复制
/**
  @this is my question , how can i make this
  var arrayItem = [" foxy  jennifer "];
  
  to this
  
  var arrayItem = ["foxy","jennifer"];
  
**/

var arrayItem = [" foxy  jennifer "];
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

仍然在弄清楚,如何使字符串在数组中,到数组中的一个独立的项目,任何人都有一个想法,如何使这种情况发生感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-29 08:32:07

尝试使用split()filter()

代码语言:javascript
复制
var arrayItem = [" foxy  jennifer "];
arrayItem = arrayItem[0].split(' ').filter(i => i);
console.log(arrayItem);

split()方法将字符串对象拆分为字符串数组,方法是将字符串拆分为子字符串,使用指定的分隔符字符串来确定每个拆分的位置。

filter()方法创建一个新数组,其中包含所有通过所提供函数实现的测试的元素。这里,只需从数组中删除所有空字符串。

OR:如果您不想使用filter(),可以执行以下操作

代码语言:javascript
复制
var arrayItem = [" foxy  jennifer "];
arrayItem = arrayItem[0].trim().split(/\s+/);
console.log(arrayItem);

票数 1
EN

Stack Overflow用户

发布于 2018-04-29 08:38:59

这是我要做的方法,详细解释。

代码语言:javascript
复制
let arrayItem = [" foxy jennifer "];

function extractWords(strList) {
  // If strList is a string transform it into an array
  if(typeof strList === 'string')
    strList = [strList];
  // If strList is still not an array throw an error
  if(!Array.isArray(strList))
    throw 'strList must be an array';
  // more validations....
    
  // Once the validations are done,
  
  let result; // Will be our resulting array of words
  
  // If strList contains only one string
  if(strList.length === 1) {
    result = strList[0].trim().split(/\s+/);
    /* trim removes the spaces at both ends of a string */
    
    /* split allows to split a string where the 
    specified string or RegExp matches */
    
    /* /\s+/ is our RegExp where \s means a space 
    and \s+ means we want to match/replace 1 or more 
    consecutive space so the string will be splitted 
    everywhere one or more spaces are found */
    
    /* For example, "a b c d " would become ["a", "b", "c", "d"] 
    and notice that the trailing space is removed by the trim 
    which prevents from getting this ["a", "b", "c", "d", ""]
    cause we dont want empty string in our array of words */
  } else {
    // Now, if strList has more than 1 string in it
    // The reduce wont work with only one string in the array
    
    // The following is a bit more complex
    
    /* reduce will pass through every string string exactly 
    like map or forEach except that 2 elements will be passed 
    to the callback function */
    result = strList.reduce(function(arg1, arg2) {
      /* The first time the callback is called, 
      arg1 will contain the first element of the array 
      but for all the next calls to the callback arg1 will contain 
      the previous call result. The whole reduce will return the 
      last result */
      /* arg2 will contains the next element in the array exactly 
      like the first argument of forEach */
      
      /* Since we want an array of words as final result we will 
      want to transform arg1 into an array */
      
      /* Here we check if arg1 is an array cause on the first call 
      arg1 will be the first element of strList and wont be an array */
      if(!Array.isArray(arg1)) {
        // Here split will return an array
        arg1 = arg1.trim().split(/\s+/);
      }
      /* We dont transform arg2 into an array since it wont 
      be passed as any argument on the next call instead on the next call arg2
      will be the next element of the array */
      
      /* The value we will return here will be the value of arg1 on 
      the next call */
      /* Here we return concatenation the previous results with an array 
      of words contained in arg2 */
      return arg1.concat(arg2.trim().split(/\s+/));
    });
    /* The final result is an array with all the words of every string in strList */
  }
  return result;
}

console.log(extractWords([" this  is  ", "just an    ", "example"]));
console.log(extractWords(arrayItem));

告诉我你还有什么不明白的,我会解释的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50084709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档