What is the best way to determine if all the elements in array2 are present in array1?

I am trying to compare the content of two arrays.

array1 = [13, 15,18,19, 25]
array2 = [{id:13, label: 'value1'},{id:15,label:'value2'},{id:25, label:'value3'}]

Is there a way to determine if all the values in array2's id property exist in array1?

Answer №1

Go through each element in array 2 and verify that the id of each element is present in array 1.

array2.every(({ id }) => array1.includes(id));

const array1 = [13, 15, 18, 19, 25];
const array2 = [{
  id: 13,
  label: 'value1'
}, {
  id: 15,
  label: 'value2'
}, {
  id: 25,
  label: 'value3'
}];

const res = array2.every(({ id }) => array1.includes(id));

console.log(res);

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

Iterating through a dataset in JavaScript

Trying to find specific information on this particular problem has proven challenging, so I figured I would seek assistance here instead. I have a desire to create an arc between an origin and destination based on given longitude and latitude coordinates. ...

Use JavaScript to limit Google Analytics and Yandex.Metrica to track only the referral sources and screen sizes

I prefer not to include external JavaScript on my website for unnecessary tracking purposes. However, I do need to gather referrer and screen size information, which cannot be achieved with a regular HTML img tag alone. What is the most standard-complian ...

Safari is capable of rendering Jquery, whereas Chrome seems to have trouble

The code I am using renders perfectly in Safari but not in Chrome. Despite checking the console in Chrome, no errors are showing up. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="ht ...

How can we effortlessly generate a times table using For loops and arrays?

As someone new to JavaScript, I am eager to learn. My goal is to create two "for" loops: one to save the values of the 6 times table up to 12x6 into an array called timesTable, and another to display these values in the console (e.g., 0 x 6 = 0). Thank y ...

Utilize various CSS styles for text wrapping

I'm struggling to figure out where to begin with this problem. My goal is to create a three-column row that can wrap below each other if they cannot fit horizontally. The height of the row should adjust to accommodate the items while maintaining a fix ...

Automatically compute and convert currency formats using JavaScript

May I ask again? Hopefully you understand. I am looking to automatically calculate with a money format. Here is a demo: https://jsfiddle.net/xp4ky2gg/ This is my code... HTML code <table style="width:100%"> ...

Tips for saving text into an array while using a foreach loop

While I have come across some related queries in similar questions posted here, I have not found anything that I could use as a reference to implement properly in my code. I have an array containing thousands of numbers which are names of files in a direct ...

JSON schema defining an array of objects that were mentioned earlier in the schema definition

I'm currently brainstorming a schema for a JSON document that consists of an array of items representing our "git repos" along with various mappings. However, I'm facing some challenges with it. { "$schema": "http://json-schema.org/draft-07 ...

Unresolved Issue: jQuery AJAX Fails to Load PHP File in Internet Explorer

I have a piece of code that runs lastactivity.php every 10000 milliseconds on FireFox, Chrome, and Opera for Windows & Linux. However, it does not seem to work on Internet Explorer (tested on IE Version 11). There are no error messages displayed by the b ...

Using JavaScript to Calculate Distance from Wifi Signal Strength (RSSI)

Has anyone discovered a reliable method for converting an RSSI signal to an accurate distance? We have experimented with various formulas, but each one yields a different outcome. One formula in particular that we've been testing is as follows: pub ...

I wish for the content with "position: fixed" to stay in place without moving when it encounters this particular class while scrolling

How can I make the content with "position: fixed" stop at the designated "LimitPoint" and not continue scrolling past it? The fixed content currently goes beyond the "LimitPoint" and disappears when scrolling to the bottom. Is there a way to achieve the ...

Stencil - React Integration Does Not Support Global CSS Styling

As per the guidance provided in the Stencil docshere, I have established some global CSS variables within src/global/variables.css. This file is currently the sole CSS resource in this particular directory. Upon attempting to incorporate my components int ...

I can't figure out why I'm receiving a TypeError stating that onSuccess is not a function within my AngularJS service

Utilizing an angularjs service that utilizes restangular for API calls. angular.module('app.userService', []) .factory('userService', ['localStorageService', '$rootScope', 'Restangular', func ...

What is the best way to acquire the opposing attribute?

There is a specific object I am working with that will always have 2 properties, never more. The object looks like this: var people = { 'John': { ... }, 'Peter': { ... } } In addition, there is a variable var name = 'John&ap ...

How to conceal certain columns in Angular Material when in mobile view

I am currently utilizing an Angular Material table: <div class="table-container"> <table mat-table [dataSource]="dataSource" class="child"> <mat-divider></mat-divider> <ng-container matColumnDef="title" ...

Creating anchor links with #id that function correctly in an Angular project can sometimes be challenging

My backend markdown compiler generates the HTML content, and I need Angular to retrieve data from the server, dynamically render the content, and display it. An example of the mock markdown content is: <h1 id="test1">Test 1<a href="#test1" title ...

Utilizing React/Redux to handle asynchronous actions within reducers efficiently through chains or promises

In my React/Redux application, I am dealing with filtering and searching functionality that requires some manipulation of data between receiving the filters and updating the search query. To manage this, I have implemented a reference map of active filters ...

Export data from Magento and transfer it to Mixpanel by converting it into JSON format

Initially, the challenge lies in extracting data from Magento and transforming it into JSON or CSV format. Subsequently, this data must be imported into Mixpanel. The provided demo from Mixpanel seems to cover the necessary class and components, but extr ...

What is the best way to combine objects that share the same id?

View Image: Displaying Image POST ID's https://i.stack.imgur.com/JO5OF.png I attempted to resolve this using jQuery each, but I am uncertain about the next steps. Check out my Code: jQuery(document).ready(function($) { var data, exampleData, , ...

Searching for information on a "contact" is proving to be a challenge as the display feature is not working properly

There's a file serving as an address book containing details about individuals formatted in this style ————————-CONTACT————————- First Name : Tony Last Name : Stark Address : Malibu Phone number : 10203044032 E-ma ...