Contrast two sets of JSON data comprised of arrays of objects

Here is an example structure of the JSON data:

{ "array_listing_1" : [
       {"name":"John","age":"18","group":"user","country":"UK","hobby":"series","sport":"football"},
{"name":"Ted","age":"20","group":"user","country":"US"}, ...]}

{ "array_listing_2" : [
       {"name":"John","age":"18","group":"admin","country":"UK","hobby":"series","sport":"football"},
{"name":"Ted","age":"20","group":"user","country":"US", sport:"tennis"},
{"name":"David","age":"20","group":"user", sport:"tennis"},...]}

{ "array_listing_3" : [
       {"name":"John","age":"18","group":"admin","country":"UK","hobby":"series","sport":"football"},
{"name":"David","age":"20","group":"user", sport:"tennis"},...]}

I am tasked with comparing two JSON Array structures containing objects. The goal is to compare objects with matching names in the two arrays and identify any differences present. For instance, when comparing array_listing_1 and array_listing_2, I aim to detect new objects added to array_listing_2. Additionally, any changes in values such as John's group transitioning from user to admin need to be identified. In the case of comparing array_listing_2 and array_listing_3, the deletion of the user Ted from array_listing_3 should be detected.

Answer №1

Give this a try:

let object1 = { "array_name1" : [
       {"name":"John","age":"18","group":"user","country":"UK","hobby":"series","sport":"football"},
{"name":"Ted","age":"20","group":"user","country":"US"}]};

let object2 = { "array_name2" : [
       {"name":"John","age":"18","group":"admin","country":"UK","hobby":"series","sport":"football"},
{"name":"Ted","age":"20","group":"user","country":"US", "sport":"tennis"},
{"name":"David","age":"20","group":"user", "sport":"tennis"}]};

let object3 = { "array_name3" : [
       {"name":"John","age":"18","group":"admin","country":"UK","hobby":"series","sport":"football"},
{"name":"David","age":"20","group":"user", "sport":"tennis"}]};
let object4 = { "array_name3" : [
       {"name":"John","age":"18","group":"admin","country":"UK","hobby":"series","sport":"football"},
{"name":"David","age":"20","group":"user", "sport":"tennis"}]};
console.log(object1.array_name1 === object2.array_name2); // direct compare

function checkEquality(x,y) {
    return JSON.stringify(x) === JSON.stringify(y);
}
console.log(checkEquality(object1.array_name1, object2.array_name2));

console.log(checkEquality(object4.array_name3, object3.array_name3));

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

Combining React state value with an HTML tag would be a great way

I am facing an issue while trying to concatenate the previous value with new data fetched from an API using a loop. Instead of getting the desired output, I see something like this: [object Object][object Object],[object Object][object Object] Here is ...

Learn the steps to successfully rotate the custom icon in Material-Ui select without impacting its functionality and making sure it remains clickable

I've been trying to incorporate my own icon for the material UI select component. I successfully replaced the default icon using the "IconComponent" attribute in MU select. However, I'm facing issues with the new icon not rotating when the menu ...

The error message "the property '_env_' is not found on the type 'Window & typeof globalThis - React / Typescript ERROR" indicates that the specified property is not present in the

I encountered an issue while working with React/TypeScript. When I perform this action within the component: <p>API_URL: {window._env_.API_URL}</p> The error message I receive is: property '_env_' does not exist on type 'Window ...

Analyzing the HTML code retrieved from a jQuery AJAX call

My goal may appear straightforward: use HTML page retrieval with $.ajax() to extract a value from it. $(function () { $.ajax({ url: "/echo/html", dataType: "html", success: function (data) { $('#data').tex ...

Is it possible to connect ng-model with a style tag?

Is it feasible to create a basic template editor using angularjs where an input field with an ng-model can be connected to a style tag to control the css on an entire page rather than applying it to a specific element with inline styling? Could something ...

Pop-up - maintain the initial value

I'm working on a modal using Bootstrap and React. Inside the modal, there's a dropdown with an initial empty option: <select class="form-control" onChange={this.handleSelectCat}> <option disabled selected></option> < ...

halt execution of npm test and erase any printed content

When I run npm test on my React project, it runs unit tests using jest and react testing library. The test logs (including console log lines added for debugging) are printed to the screen but get deleted after running the tests. It seems like the logs are ...

Generate Address from Latitude and Longitude

For my project, I am trying to convert latitude and longitude coordinates into an address. While the Google Maps API is a potential solution, it requires an XML Response which complicates the process. I came across this helpful thread on Stack Overflow d ...

What is the best method to choose the following sentence once the final * has been identified

In the navigation text, my objective is to pick out the final sentence following * For example; Home*Development*Mobil and Web Development I am only interested in selecting the last sentence after * --> (Mobil and Web Development) ...

What is the best way to toggle the visibility of a dynamically created HTML element in ASP.NET?

I'm working on a program that displays two different prices depending on whether the user is registered or not. Registered users will see both the normal price and the discounted price, while unregistered users will only see the normal price. I need t ...

Generate text in a 2D format using the power of three.js

I have a 3D model created in three.js and I need to add a set of arrows decorated with small text labels based on some data. These labels must remain in 2D. There appear to be two options available: either utilizing a separate canvas element to generate a ...

Begin by opening a single div for each instance

I want a functionality where opening one div closes all the others. I've searched around for solutions and only found jQuery options, not pure JavaScript ones. Here is my code: function openDescription(description_id) { var x = document.getEle ...

Utilize Angular to dynamically assign values from object properties to an array of objects based on property names

In my array of objects, there is a property named "modulePermissions" inside an array of objects called "tabList". I have another object called "moduleName", which has the same value as the "modulePermissions" name, and a boolean value has been assigned to ...

When attempting to evaluate JSON data on a specific computer, the function JSON

Something strange is happening and I can't seem to figure it out, causing a big issue for me. I am currently working on a .Net web application that utilizes JSON (not json2) along with other JS libraries. In one specific proxy, the function JSON.eval ...

Implementing a Bootstrap bottom popover when an image is clicked using JavaScript

Does anyone know how to display a Bootstrap bottom pophover when an image button is clicked using JavaScript? Appreciate any help! ...

Why is it that despite passing all the unit tests successfully, the function fails when used in its actual context with the same parameters?

I have created a basic API to encrypt text using the Caesar cipher in Javascript with Express.js. While running tests with Jest, it seems that all the tests are passing successfully (and a console.log of the output confirms that the resulting string matche ...

Modifying the copied array will influence changes in the original array as well

Encountering a Puzzling Dilemma: I recently created a copy of an array and was surprised to find that when I made changes to the copied array, the original array also changed. Why did this happen? const groups = [[1, 2, 3, 4 ,5], [2,3]]; const sets = ...

What causes the NodeJS* event loop to persist even after my Promise has been resolved?

Question Is there a way to make NodeJS pause the event loop until all arguments have been parsed? Info I recently discovered a solution using this and this answer, involving the use of Promise. Now, when the app encounters --password, it waits for user ...

There was a unexpected JSON response from the Django backend that triggered an alert in the Chrome

Trying to send back a JSON file to the Chrome extension for user display. The query is reaching the server without issues, and the fetched URL does return the JSON file when accessed directly. However, the Chrome extension displays an "undefined" message i ...

Ending a timer from a different function in a React component

Currently, I am delving into the world of React and attempting to create a timer component. While I have successfully implemented the start function for my timer, I am now faced with the challenge of stopping the timer using an onclick handler from another ...