无法在jQuery $ .each循环中使用splice删除数组元素

时间:2017-10-01 20:58:40

标签: javascript jquery

嗨,我有一个只有一个属性的对象" contacts"它的值是一个包含4个对象的数组,每个对象都有像email,firstname等属性......

我想通过将其电子邮件属性与给定的电子邮件相匹配来从该数组中删除特定对象。

我正在尝试使用jQuery中的$ .each循环遍历该数组,并尝试在每次迭代中匹配电子邮件,并且在匹配时我尝试使用拼接删除该对象,但它无效。

以下示例代码与我正在实施的内容类似:

//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);

此方法无效,请您告诉我如何才能完成这项工作。

注意:我不想用数据对象或myArray修改任何内容,但我想找到当前情况的解决方案

非常感谢

3 个答案:

答案 0 :(得分:1)

我建议不要使用jQuery(因为你不需要它)和任何for / while循环,并且只是吻它:

function deleteElementWithEmail(data, email) {
    return data.filter(function (current) {
      return current.email !== email
    })
}

使用您的所有代码:

//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);

答案 1 :(得分:0)

使用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);

答案 2 :(得分:0)

您可以使用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);