为什么这个'undefined'会出现在HTML中?

时间:2018-06-18 08:07:45

标签: javascript undefined

我是javascript的初学者。我正在做一个简单的js代码。 HTML输出有一个'undefined'元素。我不明白为什么会这样。此脚本用于在HTML表格中显示有关发明人的详细信息。

const inventors = [
  { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
  { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
  { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
  { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
  { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
  { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
  { first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
  { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
  { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
  { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
  { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
  { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];

const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];


const fullNames = inventors.map(function(invent) {
  return [invent.first, invent.last, invent.year, invent.passed];
});
console.log(fullNames[0][0]);
var tableCells;
var cellContent;
for (var i = 0; i < fullNames.length; i++) {
  cellContent = '';
  for (var j = 0; j < fullNames[i].length; j++) {

    cellContent = cellContent + '<td>' + fullNames[i][j] + '</td>';
  }
  tableCells = tableCells + '<tr>' + cellContent + '</td>';
}
document.getElementById('demo').innerHTML = tableCells;
<div>
  <table id="demo">

  </table>
</div>

4 个答案:

答案 0 :(得分:3)

将tableCells初始化为空字符串

var tableCells = "";

否则在第一次迭代中,值将是这样的

tableCells = /*tableCells */ "undefined" + '<tr>' + cellContent + '</td>';

const inventors = [
  { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
  { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
  { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
  { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
  { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
  { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
  { first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
  { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
  { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
  { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
  { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
  { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];

const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];


const fullNames = inventors.map(function(invent) {
  return [invent.first, invent.last, invent.year, invent.passed];
});
console.log(fullNames[0][0]);
var tableCells = "";
var cellContent;
for (var i = 0; i < fullNames.length; i++) {
  cellContent = '';
  for (var j = 0; j < fullNames[i].length; j++) {

    cellContent = cellContent + '<td>' + fullNames[i][j] + '</td>';
  }
  tableCells = tableCells + '<tr>' + cellContent + '</td>';
}
document.getElementById('demo').innerHTML = tableCells;
<div>
  <table id="demo">

  </table>
</div>

const inventors = [
  { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
  { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
  { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
  { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
  { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
  { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
  { first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
  { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
  { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
  { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
  { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
  { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];

const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];


const fullNames = inventors.map(function(invent) {
  return [invent.first, invent.last, invent.year, invent.passed];
});
console.log(fullNames[0][0]);
var tableCells;
var cellContent;
for (var i = 0; i < fullNames.length; i++) {
  cellContent = '';
  for (var j = 0; j < fullNames[i].length; j++) {

    cellContent = cellContent + '<td>' + fullNames[i][j] + '</td>';
  }
  tableCells = tableCells + '<tr>' + cellContent + '</td>';
}
document.getElementById('demo').innerHTML = tableCells;
<div>
  <table id="demo">

  </table>
</div>

答案 1 :(得分:1)

您没有将tableCells初始化为任何内容,因此当您执行tableCells = tableCells + '<tr>' + cellContent + '</td>';时,第一次迭代会生成tableCells = undefined + '<tr>' + cellContent + '</td>';

正确地初始化它:

var tableCells = '';

const inventors = [
  { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
  { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
  { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
  { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
  { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
  { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
  { first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
  { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
  { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
  { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
  { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
  { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];

const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];


const fullNames = inventors.map(function(invent) {
  return [invent.first, invent.last, invent.year, invent.passed];
});
console.log(fullNames[0][0]);
var tableCells = '';
var cellContent;
for (var i = 0; i < fullNames.length; i++) {
  cellContent = '';
  for (var j = 0; j < fullNames[i].length; j++) {

    cellContent = cellContent + '<td>' + fullNames[i][j] + '</td>';
  }
  tableCells = tableCells + '<tr>' + cellContent + '</td>';
}
document.getElementById('demo').innerHTML = tableCells;
<div>
  <table id="demo">

  </table>
</div>

答案 2 :(得分:1)

你必须初始化变量befor print var tableCells = "somthing";

然后tableCells = tableCells + '<tr>' + cellContent + '</td>';

答案 3 :(得分:1)

只需做一些小改动。当您将字符串附加到没有定义任何值的变量时。 所以最初那个变量的值是'undefined'。所以最终的字符串生成如下:undefined + ******。这就是你得到这个的原因。 只需写下

var tableCells = "";

而不是

var tableCells; 

它会起作用:

Jsfiddle链接:https://jsfiddle.net/h15qsbj2/1/