Leveraging _.some in lodash

I'm working on a Typescript code where I need to check if any items in one array are present in another array. Although I am new to lodash, I thought of using _.some for this task. However, the code is currently returning false when I expected it to return true.

let array1 = ["test", "some", "lodash"];
let array2 = ["some", "includes"];

let condition : boolean = _.some(array1, array2);

Answer №1

To determine if there are any common items between two arrays, you can utilize the intersection function:

let hasCommonItems : boolean = _.intersection(array1, array2).length > 0;

Another approach is using the some method along with a callback function to test for common items:

let hasCommonItems : boolean = _.some(array1, item => array2.includes(item))

Answer №2

Even before plain JavaScript caught up with the same methods, lodash was already setting the bar for coolness...

const arr1 = ["example", "sample", "lodash"];
const arr2 = ["sample", "includes"];

const result = arr1.some(item => arr2.includes(item));

console.log(result);

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

When using a Redux action type with an optional payload property, TypeScript may raise complaints within the reducer

In my react-ts project, I define the following redux action type: type DataItem = { id: string country: string population: number } type DataAction = { type: string, payload?: DataItem } I included an optional payload property because there are tim ...

Add a service to populate the values in the environment.ts configuration file

My angular service is called clientAppSettings.service.ts. It retrieves configuration values from a json file on the backend named appsettings.json. I need to inject this angular service in order to populate the values in the environment.ts file. Specific ...

What is the best way to shorten text in Angular?

I am looking to display smaller text on my website. I have considered creating a custom pipe to truncate strings, but in my situation it's not applicable. Here's what I'm dealing with: <p [innerHTML]="aboutUs"></p> Due to t ...

Angular 10 introduces a new feature where BehaviorSubject can now hold and emit two

Currently, I am attempting to log in using Firebase. The login system is functioning correctly; however, I am facing a challenge in retrieving the error name from the authentication service and displaying it in my login component. SignIn(email: string, pas ...

Exploring Angular 2's ngFor Directive with JSON Data

Recently diving into Angular2, I've been trying to extract data from a JSON file. While I have successfully retrieved the file using a REST client, stored it in a local variable within a component, and accessed certain properties of that variable, I&a ...

How can MongoDB insertMany be used to update existing data with the same email IDs?

Assume I have a collection of documents as follows - [ { fname: "FirstName1", lname: "LastName1", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="761736111b171f1a5815191b">[email pro ...

Setting up @cypress/code-coverage with Angular and TypeScript: A comprehensive guide

When following the instructions in Cypress documentation for @cypress/code-coverage, it recommends using the following code... // cypress/support/e2e.js import '@cypress/code-coverage/support' ...as well as... // cypress.config.ts import { defin ...

Comparison between 2D array of ArrayLists and a 3D ArrayList in Java

Looking for a way to store an unknown number of objects in a 2D array of fixed length? Consider the following scenario, where a 2D array is used: int[][] clashesMatrix = new int[noOfExams][noOfExams]; Each position in the clashesMatrix requires an ArrayL ...

Guide for inserting a new field into multipart/form-data before sending it to a different server?

Is there a way to upload a pdf to a NextJS endpoint, insert a userId field in the form-data, and then send it to another server for file storage? I have successfully forwarded the request as shown below, but I am struggling to figure out how to include an ...

Designing a filter system based on price ranges using checkboxes

import { useState } from 'react'; const Shop = ({capes}: Props)=>{ const[checkVal1, setCheckVal1]= useState(0); const[checkVal2, setCheckVal2]= useState(0); const[checkVal3, setCheckVal3]= useState(0); const[checkVal4, setCheckVa ...

Troubleshooting the issue with integrating a user-defined Cordova plugin in Ionic 2 using TypeScript

I have developed a custom Cordova plugin and I am trying to integrate it with an Ionic 2 project that is using TypeScript and Angular 2. While I have successfully added the plugin to the Ionic 2 project, I am facing issues when trying to call methods defin ...

Verify the Existence of N and Its Double

From a coding problem on Leetcode, we are asked: Given an array of integers called arr, can you find two integers N and M such that N is double the value of M (N = 2 * M)? Here is my attempted solution: class Solution { public: bool checkIfExist(vect ...

React and TypeScript: Anticipating a function call or assignment but encountered an expression instead

I've developed a signup feature using React and Redux, and have completed all the necessary actions and reducers. However, I encountered the following error: Expected an assignment or function call and instead saw an expression This particular file s ...

Ensuring that the field remains active in Angular2 even after editing it with a value

When the 3rd dropdown value is selected in the first field dropdown, it enables the 2nd field. However, when I edit and change a different value, since the 2nd field is disabled, I receive null or undefined as the value. I want the 2nd field to retain its ...

Learn how to display specific text corresponding to a selected letter from the alphabet using Vanilla JavaScript

I am extracting text from a JSON file. My goal is to enable users to click on a specific letter and have the corresponding text displayed. For example, if someone clicks on 'A', I want to show the text associated with 'A'. An example of ...

Unable to loop through using ngFor

I have a component that retrieves data from the back-end and groups it accordingly. Below is the code snippet: getRecruitmentAgencyClientPositions(): void { this._recruitmentAgencyClientsService.getRecruitmentAgencyClientPositions(this.recruitmentAge ...

How can I extend a third-party JavaScript library in TypeScript using a declaration file?

Currently, I am working on creating a .d.ts file for an external library called nodejs-driver. While I have been able to map functions and objects successfully, I am struggling with incorporating the "inherit from objects defined in the JS library" conce ...

Utilize the return function to showcase the organized outcomes on the webpage

This is my Alignments.js react component that uses the new useState and useEffect React hooks. It's working great, displaying a list of alignments: import React, {useState, useEffect} from 'react'; import './App.css'; import {Link ...

Getting the nested object property with pluck using Lodash

I have an array of objects containing character information: var characters = [ { 'name': 'barney', 'age': 36, 'salary':{'amount': 10} }, { 'name': 'fred', 'age': ...

Using PHP to merge arrays with strpos and str_replace functions

I am faced with a challenge involving 2 arrays structured as follows: $alertTypes = ['outbid', 'subAllVendorComments']; $channels = ['outbiduseSms', 'outbiduseBrowser', 'subAllVendorCommentsuseEmail', &apo ...