数组,修改独立地在数组中拆分

时间:2018-04-29 08:29:22

标签: javascript arrays sorting



/**
  @this is my question , how can i make this
  var arrayItem = [" foxy  jennifer "];
  
  to this
  
  var arrayItem = ["foxy","jennifer"];
  
**/

var arrayItem = [" foxy  jennifer "];

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

还在弄清楚,如何在数组中创建单词串,到数组中的独立项目,任何人都知道如何实现这一点,谢谢

2 个答案:

答案 0 :(得分:1)

尝试使用split()filter()

&#13;
&#13;
var arrayItem = [" foxy  jennifer "];
arrayItem = arrayItem[0].split(' ').filter(i => i);
console.log(arrayItem);
&#13;
&#13;
&#13;

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

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

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

&#13;
&#13;
var arrayItem = [" foxy  jennifer "];
arrayItem = arrayItem[0].trim().split(/\s+/);
console.log(arrayItem);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是我详细解释的方式。

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));

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