Hay una matriz como esta:
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"} ]Estoy tratando de eliminar todos los objetos donde GraderId es algún valor (:
$(function() { $("#schoolGraders").on("dblclick", function() { $.each(array,function(i,r){ if (r.GraderId == $(this).val()) { r.removeItem; } }); });el código de arriba no funciona.
Aquí está el código HTML:
<select class="form-control" id="schoolGraders" style="width: 80%; height: 200px" multiple></select>Genero las opciones así:
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); });¿Hay alguna condición en JavaScript para que pueda eliminar objetos DONDE el GraderId tiene algún valor?
Tal vez algo como esto, digamos que desea eliminar el objeto donde GraderId es 876 .
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)Puedes usar filter() :
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"} ] const filteredArr = array.filter(item => item.GraderId !== $(this).val())Luego asigna elementos a HTML usando este filteredArray
Encontré el camino, gracias a todos. Aquí quería compartirlo con todos para que todos puedan usarlo.
$("#schoolGraders").on("dblclick", function () { var deletedGraderId = $(this).val(); var schoolId = $("#selectedSchools").val(); $.each(array, function (i, r) { if (r.GraderId == deletedGraderId && r.SchoolId == schoolId) { array.splice(i, 1); } }); });