我正在尝试循环我的数组并追加到html表。我的数组的一个示例如下所示
$arrayOne =["text1", "text2", "text3"]
$arrayTwo =["", "mytext1", "mytext1"]
$arrayThree=["dummyText1", "dummyText2", "dummyText3"]
$arrayFour=["fourthText", "Fourth", "Four"]
我在这里循环
arrayOne.forEach(function(value, key){
let ExampleOne = $arrayOne[key],
ExampleTwo = $arrayTwo[key] ? $arrayTwo[key] : "",
ExampleThree = $arrayThree[key] ? $arrayThree[key] : "",
ExampleFour = $arrayFour[key] ? $arrayFour[key] : "",
$("#myModal").find("#mysection").append(<tr><td>"+ExampleOne+"</td><td>"+ExampleTwo+"</td><td>"+ExampleThree+"</td><td>"+ExampleFour+"</td>
}
打印类似
的内容text1 "" "dummytext1" "fourthText"
test2 "" "dummyText2" "Fourth"
text3 "mytext1" "dummyText3" "Four"
它跳过第二个数组中的内容。我不想摆脱null / empty&#34;&#34;值。我不希望它跳过重复的条目。我不确定为什么它会跳过内容。
答案 0 :(得分:1)
刚刚清理了代码并在JsFiddle中运行,它就可以了,你去了。
var arrayOne = ["text1", "text2", "text3"];
var arrayTwo = ["", "mytext1", "mytext1"];
var arrayThree = ["dummyText1", "dummyText2", "dummyText3"];
var arrayFour = ["fourthText", "Fourth", "Four"];
arrayOne.forEach(function(value, key) {
let ExampleOne = arrayOne[key],
ExampleTwo = arrayTwo[key] ? arrayTwo[key] : "",
ExampleThree = arrayThree[key] ? arrayThree[key] : "",
ExampleFour = arrayFour[key] ? arrayFour[key] : "";
$("#myModal").find("#mysection").append("<tr><td>" + ExampleOne + "</td><td>" + ExampleTwo + "</td><td>" + ExampleThree + "</td><td>" + ExampleFour + "</td>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myModal">
<div id="mysection">
</div>
</div>
如果你需要空格(我假设你会这样做),只需编辑你已经拥有的内联布尔语句。