打印出迭代中的每个数组值,值未定义

时间:2019-09-06 06:15:09

标签: javascript arrays

我想打印出组成数组的内容。如您所见,我在代码末尾调用console.log,并且组成数组中的每个值都是未定义的。但是,如果我在console.log语句之类的其他地方调用if,则这些值是正确的。请告诉我为什么,谢谢!

$(function() {
  var text = $('p').text().split('');
  var letter = 0;
  var composed = [];
  $.each(text, function(index, value) {
    if (/\w/.test(value)) { // if the character is alphanumeric
      var prev = index - 1;
      var next = index + 1;
      if (/\w/.test(text[prev])) {
        composed[letter] = composed[letter] + value;

        if (!/\w/.test(text[next])) {
          console.log('composed[' + letter + '] = ' + composed[letter]);
          letter++;
        }
      } else {
        composed[letter] = value;

        if (!/\w/.test(text[next])) {
          console.log('composed[' + letter + '] = ' + composed[letter]);
          letter++;
        }
      }
    } else {
      composed[letter] = value;
      console.log('composed[' + letter + '] = ' + composed[letter]);
      letter++;
    }
    console.log('composed[' + letter + '] = ' + composed[letter]); // undefined
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Test both Chinese中文 ,alphbets and numbers.</p>


4 个答案:

答案 0 :(得分:2)

console.log('composed[' + letter + '] = ' + composed[letter - 1]);

这给出了正确的值

答案 1 :(得分:1)

您组成的[字母]是“未定义”,因为先前的状态是字母++;

答案 2 :(得分:1)

也许你是这个意思?

var letter = 0;
var composed = [];

function addLetter(letter, value) {
  // your could ignore white-space and punctuation there
  if (composed[letter]) composed[letter] += value
  else composed[letter] = value;

}
$(function() {
  var text = $('p').text().split('');
  $.each(text, function(index, value) {
    if (/\w/.test(value)) { // if the character is alphanumeric
      var prev = index - 1;
      var next = index + 1;
      if (/\w/.test(text[prev])) {
        addLetter(letter, value)
        if (!/\w/.test(text[next])) {
          letter++;
        }
      } else {
        addLetter(letter,value);
        if (!/\w/.test(text[next])) {
          letter++;
        }
      }
    } else {
      addLetter(letter, value)
      letter++;
    }
  });
  console.log(composed)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Test both Chinese中文 ,alphabets and numbers 1234.</p>

答案 3 :(得分:0)

只需添加空格检查。以确保我们不操纵空间。

**if (/\w/.test(text[prev] && composed[letter]!=" "))**

-

$(function() {
  var text = $('p').text().split('');
  var letter = 0;
  var composed = [];
  $.each(text, function(index, value) {
    if (/\w/.test(value)) { // if the character is alphanumeric
      var prev = index - 1;
      var next = index + 1;
      if (/\w/.test(text[prev] && composed[letter]!=" ")) {
        composed[letter] = composed[letter] + value;

        if (!/\w/.test(text[next])) {
          console.log('composed[' + letter + '] = ' + composed[letter]);
          letter++;
        }
      } else {
        composed[letter] = value;

        if (!/\w/.test(text[next]) && composed[letter]!=" ") {
          console.log('composed[' + letter + '] = ' + composed[letter]);
          letter++;
        }
      }
    } else {
      composed[letter] = value;
      console.log('composed[' + letter + '] = ' + composed[letter]);
      letter++;
    }
    console.log('composed[' + letter + '] = ' + composed[letter]); // undefined
  });
});