Is there a way to identify the items that have been removed and added in an array by comparing an old array to a new array

Consider the following scenario:

oldArray = [
    { id: 1, position: 'DEV OM'},
    { id: 2, position: 'Senior Developer'},
]

newArray = [
    { id: 2, position: 'Senior Developer'},
    { id: 3, position: 'Junior Developer'},
]

How can we identify the items that were added and removed from these arrays?

addedItems = getAddedItems(oldArray, newArray)
removedItems = getRemovedItems(oldArray, newArray)


// AddedItems 
[
    { id: 3, position: 'Junior Developer'}
]

// RemovedItems 
[
    { id: 1, position: 'DEV OM'}
]

This process is being implemented in a typescript/angular2 application

Answer №1

One solution is to create a function that identifies the variance between two arrays:

let oldArray = [
    { id: 1, position: 'Software Engineer'},
    { id: 2, position: 'Front End Developer'}
]

let newArray = [
    { id: 2, position: 'Front End Developer'},
    { id: 3, position: 'Back End Developer'}
]

function findDifference(arrA,arrB) {
  return arrA.filter(itemA => !arrB.some(itemB => itemB.id === itemA.id));
}

console.log(findDifference(oldArray,newArray))
console.log(findDifference(newArray,oldArray))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing json into another format

I am struggling with a JSON data format issue. I have tried using Object.values and object.keys along with Array.prototype.map(), but my algorithm is not producing the desired outcome. [ { "2018-01-01": [ { "firstname": "mati", "lastname": "mati ...

How should I format my PHP arrays?

I attempted to transform it $user = array(); $user[] = array("name" => "Jesse James", "email" => "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="18727d6b6b7d587d60797568747d367b777">@example.com</a>", "phone" = ...

There was an error in the CSS syntax in the production environment due to a missed semicolon

Trying to execute the npm build command "webpack --mode=production --config ./config/webpack.config.prod.js" on our project results in an issue. The issue arises when I include the bootstrap file in my tsx file as shown below. import bs from "../../../../ ...

Attempting to eliminate any dates that have already occurred

I am faced with an array containing various dates in string format such as "2016-08-12". My goal is to eliminate any dates that have already passed by comparing them to today's date. I am using TypeScript for this task. Here is a snippet of my datoAr ...

Oh no, there seems to be an issue with accessing the 'map' property in REACT JS. It appears to

Upon attempting to delete an item, I encountered an error message stating "cannot read notes, property of undefined". Despite this issue, the map function seems to be functioning as expected. It is my belief that there may be an error within the filter fun ...

Identify any missing periods and combine the years into a single range

I am working on restructuring year ranges with gaps and consolidating them. For example, converting [{start: 2002, end: 2020}, {start: 2020, end: null}] to {start: 2002, end: null} or [{2002, 2004},{2006, 2008}, {2008, null}] to [{2002-2004}, {2006-null}]. ...

methods for merging and flattening arrays in php

Hello, I recently wrote this code in Laravel: return [ 'image' => $this->image, $this->categories()->get()->map(function ($category) { return [ $category->name => $category->pivot->image ...

Dealing with mixed data types when parsing JSON using PHP's array and

Currently, I am attempting to extract information from JSON data to display on a fanpage I am developing. The JSON file I am working with can be found here. Upon inspection of the JSON link, you will notice that the structure follows a format of [{key:valu ...

I encountered an error with the array [3,5,0,3,4]. How is it possible for this array to contain a 132 pattern?

Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] where i is less than j and j is less than k, and nums[i] is less than nums[k] which is less than nums[j]. Verify if there exists a 132 pattern ...

The chart JS label callback requires a specified type declaration

Currently, I am in the process of updating an old Angular platform to a newer version. One requirement is that everything must have its type declaration. I encountered a problem with the label callback showing this error: The error message reads: "Type &a ...

Is AWS CDK generating nested cdk.out directories during synthesis?

Whilst working on my AWS CDK project for educational purposes, I found myself immersed in learning TypeScript, node.js, npm, and all related concepts simultaneously. Despite the mishap that occurred, requiring me to restart from the Github repository rathe ...

When using Router.push() in next.js, the error TypeError: products.map is not a function may arise

Currently, I am implementing redux saga in my project. Here is how the state looks: const productList = useSelector((state: RootState) => state.productList); const { loading, error, products, page, pages } = productList; In the useEffect hook, I dispa ...

Could I potentially pause and wait for a subscription in Angular?

I'm looking to display a list of posts similar to this: Post List In order to indicate which post is favorited by a user, I need to retrieve data from two different collections in my MongoDB database. The ngOnInit function in my post-list.component.t ...

Can anyone suggest a more efficient method for looping through this multi-dimensional array in PHP?

I am currently utilizing the Solr search engine, which provides my search results in a structured format: array(2) { ["responseHeader"]=> array(3) { ["status"]=> int(0) ["QTime"]=> int(1) ["params"]=> array(5) { ...

Sometimes the correct image does not display after comparing two JSON arrays

I am currently working on comparing two Json arrays, 'all' and 'allUrl', in my code. The goal is to change the color of an image in a table row based on whether the id from array 'all' matches any id from array 'allUrl&ap ...

The Recoil Nexus encountered an error: the element type provided is not valid. It should be a string for built-in components or a class/function for composite components, but an object was

Encountered the following error message: Error - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. at ReactDOMServerRenderer.render ... This issue arose when integra ...

Tips for continuously running a loop function until retrieving a value from an API within a cypress project

Need help looping a function to retrieve the value from an API in my Cypress project. The goal is to call the API multiple times until we receive the desired value. let otpValue = ''; const loopFunc = () => { cy.request({ method: &ap ...

Is it possible to generate a specified number of divs or HTML tags with varying information using ReactJS?

Currently, I am in the process of constructing this website and I have noticed that I have 4 divs that are essentially doing the same thing. However, I find myself copying and pasting these divs 4 times, only making minor changes to 4 bits of information. ...

Sorting an array of objects in Typescript using a dynamic property name for generics

My goal is to develop a versatile, typed function that can arrange an array of objects by a numerical value housed under a dynamically named property within each object. Here is my current progress: export const sortByNumber = <T, K extends keyof T> ...

Retrieving the name of the current page in ionViewCanEnter

While working with Ionic 2, I am currently facing a challenge in identifying the name of the page that triggered the navigation (PUSHER) before entering the destination page (PUSHEE). Within the PUSHEE page, I have an ionViewCanEnter function where I need ...