【问题标题】:How should I go about making the names bold in this multidimensional array?我应该如何在这个多维数组中使名称变粗?
【发布时间】:2023-03-29 01:22:01
【问题描述】:

什么是最好的方法,保持类似的结构,只在这个数组中加粗名称?我可以让它在整个阵列上工作(我在这里),但我只是想得到名字。我对此有一个丑陋的方法的想法,但希望能在简洁明了的东西上提供一些帮助。另外,我知道这不是最好的使用方法,我应该将数组包装在一个对象等中,但这不是我需要的。这与我正在考虑的一个更大的问题有关,并且是为将来更大的事情而练习。谢谢。

var people = [ 
  [ "Daniel",30,"San Francisco",["175 lb"," 6\'0\""] ],
  [ "Deryl",29,"Seattle",["165 lb"," 5\'9\""] ], 
  [ "Mandie",29,"Seattle",["155 lb"," 5\'8\""] ], 
  [ "Elena",28,"Seattle",["145 lb"," 5\'6\""] ]
];

for(var i = 0; i < people.length; i++) {
    document.write("<br>"+"<b>");
    for(var j = 0; j < people.length; j++) {
        document.write(people[i][j]+"<br>");
    }
}

【问题讨论】:

  • 第二个循环不应该是for(var j = 0; j &lt; people[i].length; j++)吗?
  • 我相信你是对的。只在这几个星期。赞赏。

标签: javascript for-loop multidimensional-array nested


【解决方案1】:

我已将您的代码组织成一个对象数组,原因如下。首先,我认为它使代码 A LOT 更具可读性。数组数据很快就会变得混乱,尤其是当您有一段时间没有查看代码时。以这种情况为例 - 您正在记录人员姓名以及他/她的父母的姓名。你的数组看起来像这样:

['Toby', 'Chris', 'Lyndall']

从那时起,您必须记住:

array[0] //Persons name
array[1] //Fathers name
array[2] //Mothers name

其次,这意味着您可以遍历所有对象并轻松引用它们的任何属性,而不必引用也会变得混乱的多维数组。

var people = [ 
  {
    name: "Daniel",
    age: 30,
    location: "San Francisco",
    weight: "175 lb",
    height: "6\'0\""
  },
  {
    name: "Deryl",
    age: 29,
    location: "Seattle",
    weight: "165 lb",
    height: "5\'9\""
  },
  {
    name: "Mandie",
    age: 29,
    location: "Seattle",
    weight: "155 lb",
    height: "5\'8\""
  },
  {
    name: "Elena",
    age: 28,
    location: "Seattle",
    weight: "145 lb",
    height: "5\'6\""
  }
];

people.forEach(function(person) {
  var html = '';
  
  html += '<b>' + person.name + '</b>' + '<br>';
  html += person.age + '<br>';
  html += person.location + '<br>';
  html += person.weight + '<br>';
  html += person.height + '<br><br>';
  
  document.write(html);
});

【讨论】:

  • 我大致了解这种方法。无法弄清楚如何以我的方式保持它完好无损。出于更专业的目的,请记住这一点。
【解决方案2】:

把你的循环改成这个...

for(var i = 0; i < people.length; i++) {
    document.write("<br/>");
    for(var j = 0; j < people[i].length; j++) {
        var text = people[i][j];
        if (j == 0) {
            text = "<b>" + text + "</b>";
        }
        document.write(text + "<br/>");
    }
}

它的作用与您之前的相同,但它使用&lt;b&gt; 标记包装每行中的第一个元素。有更好的方法可以做到这一点,比如使用 css 和类来识别列,从长远来看,这将更容易维护和修改,但为了学习和修复这一问题,这就足够了。

此外,内部循环的长度为people,而不是特定行存在问题。我通过将其更改为 people[i].length 来解决此问题。

【讨论】:

    【解决方案3】:

    people[i][0] 是名称,所以先在&lt;br&gt; 标签内输出那个,然后再输出其余的?

    编辑: 为了便于阅读,也许您应该考虑重构您的数组以包含 JSON 对象?例如,您将能够通过people[i].name 获取此人的姓名。

    【讨论】:

    • 另外,你的内部 for 循环看起来不正确?在为每个人输出数据时迭代 people.length 次数没有意义吗?
    • 你会如何解决我的问题?看了一分钟,想不通。对编程还是很陌生。你会怎么写出来?
    【解决方案4】:

    您可以尝试在每个people 元素中使用br 标记包围数组中的第一个元素。

    var people = [ 
      [ "Daniel",30,"San Francisco",["175 lb"," 6\'0\""] ],
      [ "Deryl",29,"Seattle",["165 lb"," 5\'9\""] ], 
      [ "Mandie",29,"Seattle",["155 lb"," 5\'8\""] ], 
      [ "Elena",28,"Seattle",["145 lb"," 5\'6\""] ]
    ];
    
    for(var i = 0; i < people.length; i++) {
        var props = people[i];
        if(props.length > 0) {
            document.write('<b>' + props[0] + '</b>&nbsp;');
            for(var j = 1; j < people[i].length; j++) {
                document.write(props[j]+"<br>");
            }
        }
    }

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多