m0_71681226 2022-10-25 11:51 采纳率: 100%
浏览 72
已结题

js用splice更新数组失败。

js用splice更新数组失败。
数组:

var list = [{ "Id": '0', "FirstName": "Stark", "LastName": "Bill", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
    { "Id": '1', "FirstName": "Shown", "LastName": "Jenny", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
    { "Id": '2', "FirstName": "Geogrge", "LastName": "Hash", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
    { "Id": '3', "FirstName": "Intwre", "LastName": "zaiwoo", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
    { "Id": '4', "FirstName": "Niko", "LastName": "baby", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" }];

html:

Insert a patient
Patient ID*:<input type="text" id="insertId" value="">
First name*:<input type="text" id="FirstName" value="">
Last name*:<input type="text" id="LastName" value="">
Date of birth *:<input type="text" id="DateOfBirth" value="">
Gender*:<input type="text" id="Gender" value="">
Primary insurance*:<input type="text" id="PrimaryInsurance" value="">
Address *:<input type="text" id="Address" value="">
Contact number *:<input type="text" id="ContactNumber" value="">
Next of kin:<input type="text" id="NextOfKin" value="">
<input type="button" value="Insert a Patient" onclick="insertPatient();">

js:

function insertById(list, insertId) {
     var insertId=document.getElementById("insertId");
     var FirstName=document.getElementById("FirstName");
     var LastName=document.getElementById("LastName");
     var DateOfBirth=document.getElementById("DateOfBirth");
     var Gender=document.getElementById("Gender");
     var PrimaryInsurance=document.getElementById("PrimaryInsurance");
     var Address=document.getElementById("Address");
     var ContactNumber=document.getElementById("ContactNumber");
     var NextOfKin=document.getElementById("NextOfKin");
    for (var i = 0; i < list.length; i++) {
        if (list[i].Id === deleteId) {
            list.splice(i, 1,{Id: insertId.value,
                FirstName: FirstName.value,
                LastName: LastName.value,
                DateOfBirth: DateOfBirth.value,
                Gender: Gender.value,
                PrimaryInsurance: PrimaryInsurance.value,
                Address: Address.value,
                ContactNumber: ContactNumber.value,
            NextOfKin: NextOfKin.value});
        }
    }
    return list;
}
function insertPatient(){
     var insertId=document.getElementById("insertId");
     var res = insertById(list,insertId.value)
     console.log(res)
     refreshList();
    
}

错误:
点击insert按钮控制台数组刷新了一下但并没有更新数据。

img

请问这个问题该怎么解决

  • 写回答

4条回答 默认 最新

  • CSDN专家-showbo 2022-10-25 12:01
    关注

    不用splice,直接更新对应id记录数组项就行,并且代码缺少deleteId这个变量,改下面即可。

    
            function insertById(list, insertId) {
                var insertId = document.getElementById("insertId");
                var deleteId = insertId.value;//定义变量
                var FirstName = document.getElementById("FirstName");
                var LastName = document.getElementById("LastName");
                var DateOfBirth = document.getElementById("DateOfBirth");
                var Gender = document.getElementById("Gender");
                var PrimaryInsurance = document.getElementById("PrimaryInsurance");
                var Address = document.getElementById("Address");
                var ContactNumber = document.getElementById("ContactNumber");
                var NextOfKin = document.getElementById("NextOfKin");
                for (var i = 0; i < list.length; i++) {
                    console.log(list[i].Id === insertId, deleteId)
                    if (list[i].Id === deleteId) {///直接更新数组项
                        list[i] = {
                            Id: insertId.value,
                            FirstName: FirstName.value,
                            LastName: LastName.value,
                            DateOfBirth: DateOfBirth.value,
                            Gender: Gender.value,
                            PrimaryInsurance: PrimaryInsurance.value,
                            Address: Address.value,
                            ContactNumber: ContactNumber.value,
                            NextOfKin: NextOfKin.value
                        };
                        break;//不需要继续循环
                    }
                }
                return list;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月25日
  • 已采纳回答 10月25日
  • 创建了问题 10月25日

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目