我有一个里面有物体的array.我要做的是循环遍历数组,判断是否存在按键的值,以及是否将CSS应用于div,但现在,即使该值存在,指定的CSS也不会应用,它只应用"else"CSS.为什么会发生这种情况,我该如何解决?提前谢谢.
let testArray = [{"length": "None", "duration": "10000", "percentage": "65"}, {"width": "Half", "detail": "under", "duration": "25000", "percentage": "25"}, {"length": "Full", "duration": "20000", "percentage": "90"}]
testArray.forEach((obj) => {
if (obj.length?.toLowerCase() == 'none') {
$('.test').css('background-color', 'red')
} else {
$('.test').css('background-color', 'blue')
}
});
.test {
width: 30%;
height: 40%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='test'>
<p>testing</p>
</div>