P粉5123632332023-08-15 10:10:49
It should be obvious that this will never be true, right? Data
does not change between read accesses on either side of the comparison operator.
You need to compare the current value with the value of the previously processed record. The simplest way is to store the previous value into a variable and initialize it to a value that won't appear in the actual data (so it will return true when checked on the first record).
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>