Просто используйте Array.filter
и Array.some
, чтобы получить предметы из array1
, не связанные с array2
:
const array1 = [{ "id": "1", "name": "Test 1" }, { "id": "2", "name": "Test 2", }, { "id": "3", "name": "Test 3", } ]
const array2 = [{ "id": "12", "name": "Test 1", "associatedID": "1" }, { "id": "22", "name": "Test 2", "associatedID": "2" }, { "id": "32", "name": "Test 3", } ]
const result = array1.filter(x => !array2.some(y => y.associatedID == x.id))
console.log(result)
С помощью lodash это можно сделать также через _. Разность с :
const array1 = [{ "id": "1", "name": "Test 1" }, { "id": "2", "name": "Test 2", }, { "id": "3", "name": "Test 3", } ]
const array2 = [{ "id": "12", "name": "Test 1", "associatedID": "1" }, { "id": "22", "name": "Test 2", "associatedID": "2" }, { "id": "32", "name": "Test 3", } ]
const result = _.differenceWith(array1, array2, (x,y) => x.id === y.associatedID)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>