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

Is it true that JavaScript Date.parse doesn't recognize Alaska Timezones?

When using JavaScript's Date.parse, it handles Pacific Time without any issues: Date.parse('June 20 2015 10:22 PDT') However, it encounters issues with Alaska Time: Date.parse('June 20 2015 10:22 AKDT') Does anyone have a relia ...

Utilizing Node.js with Graphics Magick to generate high-quality cropped thumbnails without sacrificing image resolution

Is there a solution to maintain image quality when cropping and centering an image using Graphics Magick? The code I currently have reduces the fidelity of the resulting image. gm(imagePath) .thumbnail(25, 25 + '^') .gravity('Cent ...

Clicking on an image in a jQuery autocomplete menu will trigger a data post to an Express

My jquery autocomplete menu is functioning properly, displaying a list of books with author, title, and book image. I am now looking to enhance it by allowing users to click on the book image and then have the book title posted to an express app.post metho ...

What is the best way to add Vue dependency using CDN?

For my project which is built using Kendo, Vue, .Net, Angular and jQuery, I need to incorporate https://www.npmjs.com/package/vue2-daterange-picker. <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-c ...

The removal of an object becomes unsuccessful when objects with lower indices have been deleted beforehand

Attempting to construct a multi-layer object representing a program; check out my progress here http://codepen.io/Irish1/pen/lbjdw Imagine adding 3 weeks, each with 3 days, and each day having 3 sessions. Removing these objects is feasible as long as caut ...

Inquiring about Vue component prop defaults and detecting when a user has not assigned a value

1. I am looking for a way to set a default value for a component prop in Vue 2. Take, for example, a basic movies component that can be utilized like this: <movies year="2016"><movies> Vue.component('movies', { props: ['yea ...

Decoding Json Search Results from Google Shopping

After experimenting with various examples from different sources, including the web, for a couple of weeks, I've hit a roadblock. While I can successfully retrieve the desired search results from Google Shopping: { "items": [ { "product": { "title ...

In Node.js Express, the download window won't pop up unless the entire file has been sent in the header response

I have been using Express to develop an API that allows users to download PDF files. The download process is functioning correctly, but I am facing an issue where the download window, which prompts me to select a destination folder for the download, only a ...

Using V-bind to assign multiple classes has never been easier

Is there a way to assign one of two classes to an element based on three possible input variables in my Vue.js code? <input type='text' class='inputwordtext' v-bind:class="{(wordupload.firstchoice.selected == 'Zinnenlijst' ...

The webpack-bundle-analyzer tool reveals that running webpack -p does not eliminate the development dependency react-dom.development.js

Here is my custom webpack configuration: const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; const SO ...

Use Javascript to display an image based on the date, otherwise hide the div

I'm looking to implement an image change on specific dates (not days of the week, but actual calendar dates like August 18th, August 25th, September 3rd, etc). Here's the div I'm working with: <div id="matchday"> <img id="home ...

Conditional jQuery actions based on the selected radio button - utilizing if/else statements

This task seemed simple at first, but I quickly realized it's more challenging than expected. Apologies in advance, as Javascript is not my strong suit. My goal is to have the main button (Get Your New Rate) perform different actions based on whether ...

Encountered a problem with AngularUniversal prerendering: UnhandledPromiseRejectionWarning: Unable to locate NgModule metadata for 'class{}'

Objective The task may seem lengthy, but it's straightforward! Currently, I am utilizing Angular Universal for Server-Side Rendering (SSR) by following a tutorial. The Universal/express-engine has been installed, main.js is generated in the dist/pro ...

Sort through JSON data according to the current page's URL path

The issue at hand Currently, I am faced with the challenge of filtering JSON data and presenting only a specific portion of it on an Angular web page, depending on the URL of that particular page. Delving deeper into the matter Within my dataset, there ...

The functionality of Jquery UI is not compatible with version 1.12

Incorporating jQuery UI into my current project has presented some challenges. Both the jquery-ui.min.css and jquery-ui.min.js files are version 1.12, so I opted for the latest jQuery version, jquery-3.2.1.min.js. Specifically, I decided to test the datep ...

"Troubleshooting Error: When accessing a property in AngularJS,

My goal is to retrieve a date value. However, I encounter an error message saying 'Cannot read property 'NTLI' of undefined' whenever the checkbox is unchecked and the date picker is invisible. Strangely enough, everything works fine wh ...

Whenever a click event is triggered, the Vue method is executed twice

Why is the set method being executed twice? Check the console when you click the star. Removing @click="set(rating)" results in no action, indicating it is not called elsewhere. http://jsfiddle.net/q22tqoLu/ HTML <div id="star-app" v-cloak> ...

Developing a two-dimensional JavaScript array using an AJAX PHP request

I've been working with a MySQL table that stores image data. My goal is to extract this image data and store it in a JavaScript array. The fields I need for the array are "image_ref" and "image_name." To achieve this, I understand that I'll nee ...

Navigating additional information in D3 Node Link Force diagram

Recently delving into the world of D3, I have been pleasantly surprised by its capabilities and decided to experiment with the Directional Force Layout. My Objective Initially, I successfully created a json object using a for loop to prepare my items for ...

What is the best way to identify modifications in a JSON object?

import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.0 Window { visible: true width: 640 height: 480 property var json: { "name": "old" } Component.onCompleted: json.name = "new" onJsonChanged: co ...