How can I confirm in Lodash whether all properties in an array are equal even without a property to exclude?

Consider the following array of employee data:

var service = [{
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: false,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",
    }
]

To confirm that AssignedEmployeeInternalID is consistent across all elements in the array, you can use the following approach:

result =  _.differenceBy(service, [{ 'AssignedEmployeeInternalID': service[0].AssignedEmployeeInternalID }], 'AssignedEmployeeInternalID'); 

If you are unsure of the value to exclude (the specific AssignedEmployeeInternalID), and want to dynamically verify that all properties are the same, you can implement a more dynamic solution.

Answer №1

To easily find out if all array items have the same value for 'AssignedEmployeeInternalID', you can utilize the uniqBy method (refer to the documentation here):

var uniqueByID = _.uniqBy(service, 'AssignedEmployeeInternalID');
var allMatch = uniqueByID.length === 1;

Answer №2

To achieve this using vanilla JavaScript, you can utilize the Array every method in the following way:

var service = [{
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: false,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",

    },
    {
        AssignedEmployeeInternalID: "8000000011",
        AssignedEmployeeUUID: "00000000-0001-1EE1-82C1-8379F1C4462E",
        BillableIndicator: true,
        BusinessPartnerFormattedName: "Sébastien Brun",
        EndDateTime: null,
        EndDatetimeZoneCode: "UTC",
        Info: "-N/A",
        InternalID: "",
        ObjectID: "00163E0E46241EE988E20C5E4033E0BE",
        ParentObjectID: "00163E0306801ED287F1C8E9CDFA9E4A",
        PeriodPlansArray: [],
        PlannedWorkQuantity: "0.00000000000000",
        PlannedWorkQuantityunitCode: "HUR",
        PlannedWorkQuantityunitCodeText: "",
    }
];

const allSame = service.every(el => el.AssignedEmployeeInternalID === service[0].AssignedEmployeeInternalID);

console.log(allSame);

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

A solution to determining the size of an array in C++ by using a variable within square brackets

The following code snippet shows my dilemma. I am attempting to declare an array with a size represented by variable n. FILE *fp; fp=fopen("myfile.b", "rb"); if(fp==NULL){ fputs("file error", stderr); exit(1); } int* a; fread(a, 0x1, 0x4, fp); int n=*a; i ...

The pre-save function in Mongoose does not seem to be working properly when using discrimin

I am facing an issue where the pre save hook is not being called before saving the owner in mongoose. Is there a workaround for this problem? const baseOptions = { discriminatorKey: '__type', collection: 'users' } const Base = ...

steps to verify the status of a sent request

I am utilizing the contenteditable property in a p tag. My code is as follows: <p contenteditable="true" id="Option1_<?php echo $i ?>" style="width:98%;border:4px thin black; background-color:#D6D6D6;font-size:18px;color:black;padding:3px ">&l ...

Display the image on the webpage only if it is present, otherwise display no image

I want to include an image on my HTML page only if it is present on my computer (meaning I provide a path and it verifies). If the image does not exist, I do not want anything to be displayed. How can I create a function that verifies if the image exists ...

Unable to send WhatsApp messages using Java

I'm seeking assistance to tackle this issue. My plan involves sending a WhatsApp message using Java with the help of a WhatsApp Gateway. Below is the Java code: import java.net.*; import java.io.BufferedReader; import java.io.OutputStream; import java ...

Deciphering the sequence in which the hashchange event is executed

Question regarding the code snippet below: window.location.hash=1; $(window).on('hashchange', function() { alert('hello'); }); The purpose of this script is to: Set the hash location to 1 Show an alert with the message 'he ...

Is my approach to managing errors appropriate when using async/await functions in conjunction with Mongoose?

When working on my website, I heavily rely on asynchronous functions to manage various aspects such as creating articles, setting up administrator accounts, rendering views, and more. I have a routine of creating all the necessary async functions within a ...

Click the ng-focus button to focus the textarea

I am in need of a solution where I can trigger a button within the ng-repeat loop and focus on a textarea specific to that item in the loop. HTML <div ng-repeat="item in items"> <textarea focus-when="showTextarea">{{ item }}</textarea> ...

Is it possible to access an imported module at the top level scope of a file with a 'circular' dependency?

My current project has a modular structure that looks like this: /components ├── Button.js ├── Checkbox.js ├── index.js ├── DateSelect    ├── DateSelect.js    └── index.js In the /components/DateSelect/index.js f ...

Please provide the necessary environment variable

While developing my ReactJS app, I have been pondering on the process of specifying the necessary environment variables for the application. Where should I define that "my app requires a DATABASE_URL variable with a string formatted like this, and a PORT v ...

Can JSON files be used to store arrays with multiple dimensions?

I'm attempting to save a "multidimensional array" in a json file that will be used in JavaScript, essentially an array containing multiple arrays. Is this doable? If so, how can I achieve it? I tried formatting it the same way as in JavaScript, but it ...

Are you interested in creating dynamic tables/models with Sequelize?

Currently, I am exploring a theoretical question before diving into the implementation phase. The scenario is as follows: In my application, users have the ability to upload structured data such as Excel, CSV files, and more. Based on specific requirement ...

Conflict between jquery and hoverIntent libraries

I've encountered an issue with a website that was functioning properly until we added a new form to a specific page. The form, created by another person, utilizes javascript/jQuery for processing. Since adding this form, it has caused the majority of ...

What is the reason tailwind does not take precedence over locally defined styles?

I've been experimenting with changing the default text color using Tailwind CSS, but for some reason, it's not taking effect. I've noticed that Bootstrap is able to override the default style without any issues. I'm fairly new to Tailw ...

Ways to switch classes within a loop of elements in vue.js

I'm just starting to learn vue.js and I'm working on a list of items: <div class="jokes" v-for="joke in jokes"> <strong>{{joke.body}}</strong> <small>{{joke.upvotes}}</small> <button v-on:click="upvot ...

Attempting to create a button that will only appear for items with a defined value for a specific variable

I am currently facing an issue with a template that needs proper population. The store locator on my website lists pharmacies and displays their relevant information (phone number, address, hours of operation) along with three buttons (view store, view fl ...

Are the libraries referenced exclusively in getServerSideProps included in the bundle by NextJS?

I have a Next.js page where I am fetching data from a database using prisma as the ORM within the getServerSideProps() function. I'm following an example provided in this official Prisma github repository. Here's a simplified version of how my p ...

Saving an Integer Value into a Character Array in C++

Hey there! Currently, I'm working on developing a Tic Tac Toe game for my college project. The board size of the game must be configurable and implemented using a 2D array in C++. However, I'm facing some challenges with initializing default numb ...

retrieve all entries from a paginated grid

Currently, I am utilizing the datatable feature in bootstrap4 with a table that has pagination set to display 10 items per page. I have implemented a function to retrieve values from the table, however I am facing an issue where I am only able to retriev ...

Using a JavaScript onclick function to retrieve specific elements within the document

I'm attempting to extract the elements from the source that was clicked on, but for some reason it's not functioning correctly. Check out my JSFIDDLE Here's the HTML: <span class="populate" onclick="populate();" href="?id=1">Hello&l ...