2017-10-01 5 views
0

こんにちは私は "連絡先"として1つのプロパティを持つオブジェクトを持っていて、その値は4つのオブジェクトを含む配列で、

電子メールプロパティを特定の電子メールと照合することによって、その配列から特定のオブジェクトを削除したいと考えています。

jQueryの$ .eachループを使ってその配列を繰り返し処理しようとしていて、各繰り返しで電子メールを一致させようとしていますが、一致するとspliceを使ってそのオブジェクトを削除しようとしていますが、以下は

は、私が実装していますものと同様のサンプルコードです:

//main object with all the data 
var data = { 
    "contacts": [ 
    { 
     "email": "[email protected]", 
     "firstname": "Jonas", 
     "lastname": "Sultani", 
     "prefix": "Mr", 
     "title": "Consultant", 
     "company": "Helly Hansen", 
     "phone": "+49 6245 99334", 
     "fax": "+49 6245 99335" 
    }, 
    { 
     "email": "[email protected]", 
     "firstname": "James H", 
     "lastname": "Simmons", 
     "prefix": "Mr", 
     "title": "AP Lead", 
     "company": "Boeing", 
     "phone": "+1 112-445-6684", 
     "fax": "" 
    }, 
    { 
     "email": "[email protected]", 
     "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 = "[email protected]"; 

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

あなたがポジションチェンジするので、インデックスの意味を変えているループ内で配列を変化させた場合。結果は '$ .each()'の実装の詳細に依存しています。 ES 2015の配列メソッド 'map'と' filter'を使って新しい配列を作るのはどうですか? –

+1

ここで示唆しているように '$ .grep()'を試すことができます:https://stackoverflow.com/a/4869347/3450689 –

+0

はい、ただ最初の条件が満たされたので、反復、一致、削除、ブレーク。私は、ループ内のシャッフルされた配列を続行したくありません。 –

答えて

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); 
1
(あなたがそれを必要としないので)私はjQueryの使用に対して助言する

および任意のために/しばらくトイレに対するP、およびそれをKISS:すべてのコード付き

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

//main object with all the data 
var data = { 
    "contacts": [ 
    { 
     "email": "[email protected]", 
     "firstname": "Jonas", 
     "lastname": "Sultani", 
     "prefix": "Mr", 
     "title": "Consultant", 
     "company": "Helly Hansen", 
     "phone": "+49 6245 99334", 
     "fax": "+49 6245 99335" 
    }, 
    { 
     "email": "[email protected]", 
     "firstname": "James H", 
     "lastname": "Simmons", 
     "prefix": "Mr", 
     "title": "AP Lead", 
     "company": "Boeing", 
     "phone": "+1 112-445-6684", 
     "fax": "" 
    }, 
    { 
     "email": "[email protected]", 
     "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 = "[email protected]"; 

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

あなたが希望する結果を得るためにarray.filter機能を使用することができます。

//main object with all the data 
 
var data = { 
 
    "contacts": [{ 
 
     "email": "jo[email protected]", 
 
     "firstname": "Jonas", 
 
     "lastname": "Sultani", 
 
     "prefix": "Mr", 
 
     "title": "Consultant", 
 
     "company": "Helly Hansen", 
 
     "phone": "+49 6245 99334", 
 
     "fax": "+49 6245 99335" 
 
    }, 
 
    { 
 
     "email": "[email protected]", 
 
     "firstname": "James H", 
 
     "lastname": "Simmons", 
 
     "prefix": "Mr", 
 
     "title": "AP Lead", 
 
     "company": "Boeing", 
 
     "phone": "+1 112-445-6684", 
 
     "fax": "" 
 
    }, 
 
    { 
 
     "email": "[email protected]", 
 
     "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 = "[email protected]"; 
 

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