[英]Unable to delete an array element using splice in jQuery $.each loop
Hi I have an object which has only one property as "contacts" and its value is an array which contains 4 objects and each object has properties like email, firstname etc... 嗨,我有一个对象只有一个属性作为“联系人”,它的值是一个包含4个对象的数组,每个对象都有诸如电子邮件,名字等属性。
I want to remove a particular object from that array by matching its email property with an given email. 我想通过将其电子邮件属性与给定电子邮件进行匹配来从该数组中删除特定对象。
I am trying to iterate through that array using $.each loop in jQuery and trying to match email in each iteration and when matched I am trying to delete that object using splice but it is not working. 我试图使用$ .each循环在jQuery中遍历该数组,并尝试在每次迭代中匹配电子邮件,当匹配时,我试图使用splice删除该对象,但是它不起作用。
Below is a sample code similar to what I am implementing: 下面是与我正在实现的示例代码相似的代码:
//main object with all the data
var data = {
"contacts": [
{
"email": "jonas.sultani@hellyhansen.com",
"firstname": "Jonas",
"lastname": "Sultani",
"prefix": "Mr",
"title": "Consultant",
"company": "Helly Hansen",
"phone": "+49 6245 99334",
"fax": "+49 6245 99335"
},
{
"email": "james.simmons@boeing.com",
"firstname": "James H",
"lastname": "Simmons",
"prefix": "Mr",
"title": "AP Lead",
"company": "Boeing",
"phone": "+1 112-445-6684",
"fax": ""
},
{
"email": "slmarino@boehringer-ingelheim.com",
"firstname": "Stephanie",
"lastname": "Marino",
"prefix": "Mrs",
"title": "Project Manager",
"company": "Boehringer Ingelheim",
"phone": "+1 650-554-5124",
"fax": ""
}
]
}
//extracting array from the data object
var myArray = data.contacts;
//sample email to match and delete the object
var email = "jonas.sultani@hellyhansen.com";
//function to delete the object containing the passed email
function deleteElement(myId){
//iterating the myArray to check the email with the given email
$.each(myArray, function(key, val){
var email = val.email;
//if the email is matched the particular object on the current index in the array is deleted using splice
if(myId === email){
myArray.splice(key,1);
return;
}
});
}
//calling the function and passing the email to delete the object
deleteElement(email);
//printing the modified array
console.log(myArray);
This method is not working so can you please let me know how can I make this work. 此方法无效,请您让我知道如何进行这项工作。
Note : I don't want to modify anything with the data object or myArray but I want to find solution with the current situation 注意 :我不想使用数据对象或myArray进行任何修改,但是我想找到当前情况的解决方案
Big Thanks 太谢谢了
I would advise against the use of jQuery (because you don't need it) and against any for/while loop, and just KISS it : 我建议不要使用jQuery(因为您不需要它)和任何for / while循环,而只需亲吻一下即可:
function deleteElementWithEmail(data, email) {
return data.filter(function (current) {
return current.email !== email
})
}
With all your code: 使用所有代码:
//main object with all the data
var data = {
"contacts": [
{
"email": "jonas.sultani@hellyhansen.com",
"firstname": "Jonas",
"lastname": "Sultani",
"prefix": "Mr",
"title": "Consultant",
"company": "Helly Hansen",
"phone": "+49 6245 99334",
"fax": "+49 6245 99335"
},
{
"email": "james.simmons@boeing.com",
"firstname": "James H",
"lastname": "Simmons",
"prefix": "Mr",
"title": "AP Lead",
"company": "Boeing",
"phone": "+1 112-445-6684",
"fax": ""
},
{
"email": "slmarino@boehringer-ingelheim.com",
"firstname": "Stephanie",
"lastname": "Marino",
"prefix": "Mrs",
"title": "Project Manager",
"company": "Boehringer Ingelheim",
"phone": "+1 650-554-5124",
"fax": ""
}
]
}
//extracting array from the data object
var myArray = data.contacts;
//sample email to match and delete the object
var email = "jonas.sultani@hellyhansen.com";
//function to delete the object containing the passed email
function deleteElementWithEmail(data, email) {
return data.filter(function (current) {
return current.email !== email
})
}
//calling the function and passing the email to delete the object
myArray = deleteElementWithEmail(myArray, email);
//printing the modified array
console.log(myArray);
Use a do..while
loop or while
loop to remove elements from an array within a loop 使用do..while
循环或while
循环从循环内的数组中删除元素
let i = 0;
let len = data.contacts.length;
do {
var email = data.contact[i].email;
if (myId === email) {
data.contacts.splice(i, 1);
break;
}
++i;
} while (i < len);
You can use array.filter
function to get desired result. 您可以使用array.filter
函数获得所需的结果。
//main object with all the data var data = { "contacts": [{ "email": "jonas.sultani@hellyhansen.com", "firstname": "Jonas", "lastname": "Sultani", "prefix": "Mr", "title": "Consultant", "company": "Helly Hansen", "phone": "+49 6245 99334", "fax": "+49 6245 99335" }, { "email": "james.simmons@boeing.com", "firstname": "James H", "lastname": "Simmons", "prefix": "Mr", "title": "AP Lead", "company": "Boeing", "phone": "+1 112-445-6684", "fax": "" }, { "email": "slmarino@boehringer-ingelheim.com", "firstname": "Stephanie", "lastname": "Marino", "prefix": "Mrs", "title": "Project Manager", "company": "Boehringer Ingelheim", "phone": "+1 650-554-5124", "fax": "" } ] } //extracting array from the data object var myArray = data.contacts; //console.log(myArray); //sample email to match and delete the object var email = "jonas.sultani@hellyhansen.com"; //function to delete the object containing the passed email function deleteElement(myId) { myArray = myArray.filter(function(el) { return el.email != myId; }); } //calling the function and passing the email to delete the object deleteElement(email); //printing the modified array console.log(myArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.