gpt4 book ai didi

javascript - 如何删除 JavaScript 中的对象?

转载 作者:行者123 更新时间:2023-12-04 13:06:40 25 4
gpt4 key购买 nike

有这样一个数组:

var array = [
{SchoolId: 2 ,GraderId: 465 , SchoolGraderName: "Example1256"},
{SchoolId: 2 ,GraderId: 654,SchoolGraderName: "Example45"},
{SchoolId: 2 ,GraderId: 876,SchoolGraderName: "Example895"},
{SchoolId: 34 ,GraderId: 796,SchoolGraderName:"Example2156"},
{SchoolId: 45 ,GraderId: 356,SchoolGraderName:"Example315"},
{SchoolId: 45 ,GraderId: 457,SchoolGraderName:"Example56715"}
{SchoolId: 45 ,GraderId: 678,SchoolGraderName:"Example37675"}
{SchoolId: 45 ,GraderId: 465 ,SchoolGraderName:"Example97685615"}
]

我正在尝试删除 GraderId 为某个值的整个对象(:

$(function() {
$("#schoolGraders").on("dblclick",
function() {
$.each(array,function(i,r){
if (r.GraderId == $(this).val()) {
r.removeItem;
}
});
});

上面的代码不起作用。

这是 HTML 代码:

<select class="form-control" id="schoolGraders" style="width: 80%; height: 200px" 
multiple></select>

我生成这样的选项:

item = "";
$.ajax({
type: "GET",
url: "address" + $(this).val(),
contentType: "application/json",
dataType: "json"
}).done(function (res) {
var iteem = "";
$.each(res,
function (i, r) {
iteem += '<option value="' + r.id +
'">' + r.title + '</option>';
});
$("#graderSchools").html(iteem);
});

JavaScript 中是否存在任何条件,以便我可以删除 GraderId 具有某个值的对象?

最佳答案

也许是这样的,假设您要删除 GraderId876 的对象。

var array = [
{SchoolId: 2 ,GraderId: 465 , SchoolGraderName: "Example1256"},
{SchoolId: 2 ,GraderId: 654,SchoolGraderName: "Example45"},
{SchoolId: 2 ,GraderId: 876,SchoolGraderName: "Example895"},
{SchoolId: 34 ,GraderId: 796,SchoolGraderName:"Example2156"},
{SchoolId: 45 ,GraderId: 356,SchoolGraderName:"Example315"},
{SchoolId: 45 ,GraderId: 457,SchoolGraderName:"Example56715"},
{SchoolId: 45 ,GraderId: 678,SchoolGraderName:"Example37675"},
{SchoolId: 45 ,GraderId: 465 ,SchoolGraderName:"Example97685615"}
]

array.forEach((item, index) => {
if(item.GraderId === 876){
delete array[index]
}
})

console.log(array)

关于javascript - 如何删除 JavaScript 中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69069798/

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