I've got two color arrays and I want to return those objects which are in local array and are not available in API response.
API resposne array
[
{
"id": 25,
"color": "#00ad02"
},
{
"id": 28,
"color": "#e1b12c"
}
]
Local array
[
{
"id": 26,
"color": "#00ad02",
},
{
"color": "#e1b12c",
"id": 28,
}
]
Loop over both arrays and return the one which are not contained by the API response array. Here I am seeking output of id 25.
CodePudding user response:
The array's filter and some method can be used to find the elements which does not exist in another array.
The filter method creates a new array with all elements that pass the test implemented by the provided function. Read here
The some method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array. Read here
const notFoundElements = aprResponse.filter(
(r) => !localResponse.some((l) => l.id === r.id)
);
CodePudding user response:
apiResponsesArr.filter(resValue => !localArr.some(localValue => localValue.id===resValue.id))
