Exploring discrepancies in JSON arrays using Lodash

Using lodash to find the difference between two arrays:

c1Arr contains:

[ { varName: 'city', varValue: 'cccccccc' },
  { varName: 'country', varValue: 'dddddddd' }
 ]

c2Arr contains:

[ { varName: 'abc', varValue: 'aaa' },
  { varName: 'city', varValue: 'cccccccc' },
  { varName: 'country', varValue: 'dddddddd' }
 ]

When running this code snippet:

    var dArr = _.difference(c2Arr, c1Arr);
    console.log(dArr);

Instead of getting the expected result:

[ { varName: 'abc', varValue: 'aaa' }]

I am getting (i.e. c2Arr in total):

[ { varName: 'abc', varValue: 'aaa' },
  { varName: 'city', varValue: 'cccccccc' },
  { varName: 'country', varValue: 'dddddddd' }
 ]

Answer №1

To handle objects as elements, you must define the comparator function.

Give this a try:

_.differenceWith(c2Arr, c1Arr, _.isEqual);

var c1Arr = [{
  varName: 'city',
  varValue: 'cccccccc'
}, {
  varName: 'country',
  varValue: 'dddddddd'
}]

var c2Arr = [{
  varName: 'abc',
  varValue: 'aaa'
}, {
  varName: 'city',
  varValue: 'cccccccc'
}, {
  varName: 'country',
  varValue: 'dddddddd'
}]

var dArr = _.differenceWith(c2Arr, c1Arr, _.isEqual);
console.log(dArr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

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

Arranging information extracted from an XML document following an ajax request

Here is a snippet of XML data sample to work with: <?xml version="1.0" encoding="ISO-8859-1"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>U ...

Unexpected date outcomes in JavaScript when using a similar date format

Why is it that the first input produces the correct result, while the second input displays a time that is 5 hours behind? new Date("2000-1-1") Sat Jan 01 2000 00:00:00 GMT-0500 (EST) new Date("2000-01-01") Fri Dec 31 1999 19:00:00 GMT-0500 (EST) How can ...

The Iframe contains its own scroll feature within the body

I am facing an issue with displaying an Iframe inside a modal. The challenge is that the content within the Iframe varies in height, making it difficult to set a fixed height for the Iframe itself. When I try setting the height to 100%, the result is a 150 ...

The functionality of Jquery Ajax is limited to a single use

I'm having an issue with a page that is supposed to reload the data within a div using jQuery, but it only updates once. Here's a snippet of my code: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0 ...

Is the JSONArray empty or not?

How can I determine if the JsonArray existTeamMemberships is null or not? I want to use an IF statement after this code to check if the JsonArray is null. Can someone help me with this? JsonArray existTeamMemberships = (JsonArray) userQueryResponse.getR ...

"Securing your login form: techniques for validating with JSON, Ajax, and PHP

<?php header('Access-Control-Allow-Origin: *');//This script is intended for Cross Domain AJAX requests mysql_connect("localhost","root","root") or die("Unable to connect to database"); mysql_select_db("demo"); echo "Database c ...

React: The getDerivedStateFromProps method does not allow the invocation of functions

I can't seem to figure out why I keep getting a TypeError - Cannot read property 'getTodosList' of null when trying to call the getTodosList function inside the getDerivedStateFromProps method. Furthermore, after implementing the getDerived ...

Effectively detect the 'scrollend' event on mobile devices

When implementing the -webkit-overflow-scrolling: touch; style on a mobile element, dealing with scroll events can be quite challenging as they are triggered by various actions such as 'flicking', 'panning' and when the scroll comes to ...

Mistaken deserialization of JSON input into an incorrect object class

In my code, I have defined two classes that represent JSON responses for error and success scenarios. private class SuccessResponse { public string message { get; set; } public bool sent { get; set; } public string id { get; set ...

What could be causing appendChild to malfunction?

I'm having an issue trying to create three elements (parent and one child) where the third element, an <a> tag, is not appending to modalChild even though it's being created correctly. modal = document.createElem ...

Issue with second button in Angular Onclick() not been resolved

I'm experiencing an issue with the onClick() methods in my code. There are two onClick() methods present on the UI, but when I click on the second one, it does not call the method while the first one does. I am struggling to figure out why this is hap ...

Steps for integrating a valid SSL certificate into a Reactjs application

After completing my ReactJS app for my website, I am now ready to launch it in production mode. The only hurdle I face is getting it to work under https mode. This app was developed using create-react-app in a local environment and has since been deployed ...

Tips for updating nested data in mongoose with 2 unique identifiers?

Is there anyone out there with experience in making a Node push function work on nested objects beyond just one level? I'm looking to delve deeper into a second id within the DB model. // Functional code for updating a user at one level with one id ...

Securely Saving JWT Tokens from Auth0 Login in Node.js Express

As a novice in the world of Auth0, I am currently working on integrating it into my regular express web application. My main goal is to secure and validate users before they are able to access certain endpoints. From what I have gathered, this can be achie ...

The REST API endpoint links for Timestamp are displaying a 404 error message indicating they cannot be

After recently diving into backend development, I ran some tests locally using Postman and localhost which seemed to work perfectly. However, upon uploading my code to GitHub, I encountered a 404 error when clicking on the API endpoint hyperlinks from inde ...

The sorting icon cannot be substituted with jQuery, AJAX, or PHP

Currently, I am working on implementing "sort tables" using ajax, jquery, and PHP. The sorting function is functioning correctly; however, I need to show/hide the "sorting images". At the moment, only one-sided (descending) sorting is operational because I ...

I need to figure out how to nest a div inside of a ul-li-a and ensure that the text inside the

In my search button, I have a cached list of options. When I select one, it should trigger the menu and choose the correct option. However, I am having trouble comparing the innerText of a div with the selected option from the search area. Both a and div e ...

Step-by-step guide on how to index timestamp type using Knex.js

I'm in the process of indexing the created_at and updated_at columns using knex js. However, when I try to use the index() function, I encounter the following error: Property 'index' does not exist on type 'void' await knex.sche ...

Identifying a specific string value in an array using jQuery or JavaScript

Currently, I am working on checking for duplicate values in an array that I have created. My approach involves using a second array called tempArray to compare each value from the original array (uniqueLabel) and determine if it already exists. If the val ...

Strategies for handling axios responses within a useEffect hook in Reactjs

Currently, I am working with Reactjs and implementing nextjs. I am facing an issue where I am using Axios for fetching data from an API, but I am struggling to display it on the page. To debug this, I have tried using console.log inside the useEffect fun ...