2016-08-16 84 views
-1

我有兩個這樣的對象。如何使用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' 
    }]; 

我想刪除結果已與所有三個屬性匹配找到對象的對象。我想要的結果應該是這樣的:

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


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

你能協助嗎?

+0

請張貼你的努力 – mplungjan

+0

你確定多麼希望最終結果看起來像嗎?我的意思是你拿一個licenseId並把它作爲一個batchId。最終的結果是,你有一個3456的licenseId,但是這在起始數組中並不存在。 –

回答

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>