gpt4 book ai didi

javascript - 如何从数组中删除一个值? javascript

转载 作者:行者123 更新时间:2023-12-01 01:59:24 26 4
gpt4 key购买 nike

我这样创建一个对象:

var savingArray = new Array({doctorId: "something", username: "something", password: "something", givenName: "something", familyName: "something", address: "something", zip:"something", emailAddress: "something", phoneNumber: "something", labs: "something", defaultLab: "something"});

现在我希望删除对象的每个值...我希望最终得到 savingArray 这样:

savingArray == ({doctorId: "", username: "", password: "", givenName: "", familyName: "", address: "", zip:"", emailAddress: "", phoneNumber: "", labs: {}, defaultLab: ""});

所以我想要的就是将所有值更改为“”动态..

savingArray 从来没有相同的对象,所以不能这样做:

savingArray[0].doctorId = "";

最佳答案

for(var i = 0; i < savingArray.length; i++) {
var obj = savingArray[i];
for(var key in obj) {
obj[key] = '';
}
}

或者,由于您使用的是 jQuery,您还可以使用其 $.each() 函数进行迭代:

$.each(savingArray, function(i, obj) {
$.each(obj, function(key, value) {
obj[key] = '';
});
});

如果修改了 Object.prototype (例如,向其中添加了方法 - 通常不应该这样做),您需要添加 if(obj.hasOwnProperty(key )) 检查并仅在检查成功时设置该值。

关于javascript - 如何从数组中删除一个值? javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com