我试图循环我的数组并附加到一个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“”值。我不希望它跳过重复的条目。我不确定为什么它会跳过内容。
刚刚清理完代码并在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>
如果你需要空格(我假设你会这样做),只需编辑你已经拥有的内联布尔语句。