2016-08-16 10 views
-1

私はこのような2つのオブジェクトを持っています。JavascriptまたはLodashを使用してarrayListから特定のオブジェクトを削除する方法

var find = [{ 
    licenseId: 'A123', 
    batchId: '123', 
    name: 'xxx' 
}, 
    { 
     licenseId: 'B123', 
     batchId: '124', 
     name: 'yyy' 
    }]; 


var result = [ 
    { 
     licenseId: 'A123', 
     batchId: '123', 
     name: 'xxx', 
     tag: 'college', 
     sem: 'fourth' 
    }, 

    { 
     licenseId: 'B123', 
     batchId: '124', 
     name: 'yyy', 
     tag: 'college', 
     sem: 'third' 
    }, 
    { 
     licenseId: '1111', 
     batchId: 'C123', 
     name: 'yyy', 
     tag: 'college', 
     sem: 'second' 
    }, 


    { 
     licenseId: '3456', 
     batchId: 'B123', 
     name: 'yyy', 
     tag: 'college', 
     sem: 'second' 
    }]; 

私は検索オブジェクトのすべての3つのプロパティと一致している結果のオブジェクトを削除します。結果は次のようになります:

[{ 
     licenseId: '1111', 
     batchId: 'C123', 
     name: 'yyy', 
     tag: 'college', 
     sem: 'second' 
    }, 


    { 
     licenseId: '3456', 
     batchId: 'B123', 
     name: 'yyy', 
     tag: 'college', 
     sem: 'second' 
    }]; 

お手伝いできますか?

+0

あなたの努力を投稿してください – mplungjan

+0

最終結果をどのように見せたいのですか?私はライセンスIDを取得してバッチIDとして入れることを意味します。そして最終的には3456のライセンスIDを持っていますが、それは起動配列にも存在しません。 –

答えて

1

次のコードが有効です。

for(var j=0;j < find.length;j++){ 
    for (var i = 0; i < result.length; i++) { 
    if ((result[i].licenseId == find[j].licenseId) && 
     (result[i].name == find[j].name) && 
     (result[j].batchId == find[j].batchId)) { 
     result.splice(i, 1); 
     break; 
    } 
    } 
} 
1

あなたは結果の配列が一致する要素を持っている場合は検索し、配列findメソッドを使用することができます。ここでlicenseIdは、結果配列に同じ要素が含まれているかどうかを調べるために使用されます。

インデックスが見つかった場合は、index引数を使用してください。次に、spliceを使用して特定の要素を削除します。

はまた、あなたは、私が使用しfind配列

var find = [// json objects]; 

var result = [// json objects]; 
find.forEach(function(item){ 

var _findInResult = result.find(function(itemInResult,index){ 
    if(itemInResult.licenseId == item.licenseId){ 
    result.splice(index,1); 
    } 
    return itemInResult.licenseId == item.licenseId 
}) 

}) 
console.log(result) 

JSFIDDLE

0

をループに配列forEachを使用することができますremovesome

_.remove(result, function(obj) { 
    return _.some(find, { 
     licenseId: obj.licenseId, 
     batchId: obj.batchId, 
     name: obj.name, 
    }); 
}); 

var find = [{ 
 
    licenseId: 'A123', 
 
    batchId: '123', 
 
    name: 'xxx' 
 
}, 
 
{ 
 
    licenseId: 'B123', 
 
    batchId: '124', 
 
    name: 'yyy' 
 
}]; 
 

 
var result = [ 
 
{ 
 
    licenseId: 'A123', 
 
    batchId: '123', 
 
    name: 'xxx', 
 
    tag: 'college', 
 
    sem: 'fourth' 
 
}, 
 
{ 
 
    licenseId: 'B123', 
 
    batchId: '124', 
 
    name: 'yyy', 
 
    tag: 'college', 
 
    sem: 'third' 
 
}, 
 
{ 
 
    licenseId: '1111', 
 
    batchId: 'C123', 
 
    name: 'yyy', 
 
    tag: 'college', 
 
    sem: 'second' 
 
}, 
 
{ 
 
    licenseId: '3456', 
 
    batchId: 'B123', 
 
    name: 'yyy', 
 
    tag: 'college', 
 
    sem: 'second' 
 
}]; 
 

 
_.remove(result, function(obj) { 
 
    return _.some(find, { 
 
    licenseId: obj.licenseId, 
 
    batchId: obj.batchId, 
 
    name: obj.name, 
 
    }); 
 
}); 
 

 
console.log(result);
<script src="https://cdn.jsdelivr.net/lodash/4.15.0/lodash.min.js"></script>

関連する問題