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

标签 javascript undefined

我是 javascript 的初学者。我正在写一个简单的js代码。 HTML 输出有一个“未定义”元素。我不明白它为什么来。该脚本用于在 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>

最佳答案

将 tableCell 初始化为空字符串

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>

关于javascript - 为什么这个 'undefined' 出现在 HTML 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50905116/

相关文章:

javascript - 我将如何实现 stackoverflow 的悬停对话框?

javascript - bookshelf.js 中的多表关系

php - Ajax + 变量未定义

C错误: Identifier <funtion_from_other_header> is undefined

c++ - 未初始化的本地字符的行为?

javascript - 对象属性未定义 (javascript)

javascript - 在网站上运行 node.js 但不在本地主机上运行

javascript - 如何单击 Selenium IDE 中的嵌入式警报?

javascript - 变量声明的 Mac 终端 Node REPL 输出

variables - 如何检查变量是否在 Octave 中定义?