Case Study 1
A situation arises where an API I am using returns an array with duplicate values.
var originalArray = [{id:1, value:23},{id:1, value:24},{id:1, value:22},{id:2, value:26},{id:3, value:26}]; //example
Inquiry 1 -> How can I remove the duplicates to achieve the desired outcome:
var newArray = [{id:1, value:23 },{id:2, value:26},{id:3, value:26}];
Case Study 2
During my pagination process, a new array is fetched.
var fetchedArray = [{id:2, value:27},{id:2, value:28},{id:3, value:29},{id:4, value:28},{id:5, value:23}]; //example
I then iterate through the new array and add its elements to another array.
var targetArray = [{id:1, value:23},{id:2, value:27},{id:3, value:27}];
angular.forEach(fetchedArray, function(item){
targetArray.push(item)
});
Inquiry 2 -> What measures can be taken to prevent an object in the newly fetched array from being added to the target array if it already exists there?
Desired outcome:
var targetArray = [{id:1, value:23},{id:2, value:27},{id:3, value:27},{id:4, value:28},{id:5, value:23}]