P粉5123632332023-08-15 10:10:49
这应该很明显,这永远不会成立,对吧?在比较运算符两边的读取访问之间,Data
不会发生变化。
您需要将当前值与之前处理的记录的值进行比较。最简单的方法是将之前的值存储到一个变量中,并将其初始化为实际数据中不会出现的值(这样在第一条记录上检查时将返回true)。
var data = [{ Designacao: "Micro-ondas1", Capitulo: "Cozinha", Data: "2023-08-12", }, { Designacao: "Exaustor cinzento 1", Capitulo: "", Data: "2023-08-12", }, { Designacao: "Mesa - Castanha -8 Lugares", Capitulo: "Sala", Data: "2023-08-13" }, { Designacao: "cama", Capitulo: "Quarto", Data: "2023-08-13", }, ]; var linha = ``; var prevData = null; // initialize Object.keys(data).forEach(i => { Designacao = data[i].Designacao; Capitulo = data[i].Capitulo; Data = data[i].Data; if (Data !== prevData) { // compare; for safety using strict type checking linha += `<div class="card-header">Data Passagem de Turno - ${Data}</div>`; } prevData = Data; // update, so that this will hold the correct "previous" record on the next iteration linha += `<div class="row col-md-12"> <div class="col-md-4"> <p class="form-label">Data </p> <input type="text" class="form-control" name="dataen" value="${Data}"> </div> <div class="col-md-3"> <p class="form-label">Código Utente </p> <input type="text" class="form-control" name="codigoen" value="${Capitulo}" disabled="disabled"> </div> <div class="col-12"> <p class="form-label">Diário de Enfermagem </p> <textarea rows="6" class="form-control" name="didiarenf" > ${Designacao} </textarea> </div>`; $(".histpturno").html(linha); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="histpturno"></div>