Check if any element from the first array exists in any nested array of the second array and return a Boolean

Having two distinct types of arrays,

firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}]
.

The second object takes the form of

secondObject = {someAttribute: bla, numbers: [23, 26, 27, 28}

My goal is to determine if any of the provided number fields in the firstArrayObject are not present in the secondObject's numbers array. If a number is not found, I will halt the search and return true. In this particular scenario, it would result in true, as "25" is missing from secondObject.numbers

Struggling with the various methods such as includes, filter, some that ES6 offers.

Hoping for assistance on resolving this issue.

Answer №1

Great job so far using some:

const outcome = firstArrayObject.some(
    ({value}) => !secondData.values.includes(value)
);

const firstArrayObject = [{title: "example", value: 17}, {title: "test", value: 19}];
const secondData = {description: "random", values: [17, 20, 22, 23]};

const outcome = firstArrayObject.some(({value}) => !secondData.values.includes(value));

console.log(outcome);

This code checks if any of the elements in firstArrayObject meet the condition 'the value is not present in the array `secondData.values'."

I used destructuring for clarity, but it's optional:

const outcome = firstArrayObject.some(
    (item) => !secondData.values.includes(item.value)
);

If the secondData.values array was very large, you could optimize performance by creating a Set with the existing values to leverage the faster lookup time of the has function compared to includes:

const valuesSet = new Set(secondData.values);
const outcome = firstArrayObject.some(
    ({value}) => !valuesSet.has(value)
);

const firstArrayObject = [{title: "sample", value: 17}, {title: "demo", value: 19}];
const secondData = {description: "test", values: [17, 20, 22, 23]};

const valuesSet = new Set(secondData.values);
const result = firstArrayObject.some(
    ({value}) => !valuesSet.has(value)
);

console.log(result);

Answer №2

Here is a helpful solution using the forEach and includes array functions in JavaScript. Check out this resource for more information on JavaScript array methods.

var answer = false;
const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];

const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};

function func(obj) {
    if(!secondObject.numbers.includes(obj.number)) answer = true;
}

firstArrayObject.forEach(func);
console.log(answer)

Edit: The 'func' function can be integrated directly into the forEach parameter, although separating it as shown above could enhance readability for beginners.

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

The operation is unable to be executed in an external document

Working on a WordPress page, I am utilizing the Google Maps API. The functions in my file are as follows: function custom_map_style() { // Enqueue Google Maps API wp_enqueue_script('maps', 'https://maps.googleapis.com/maps/api/js? ...

Using AJAX to update content based on selected value in a dropdown menu

My challenge lies in ensuring that a select box retains the selected value from a database entry and triggers an onchange event during form editing or updating. While I have successfully populated the data in the select box based on other selections, I a ...

Exploring the functionality of Angular.js through QUnit testing

Is it possible to integrate angular.mock.inject() with QUnit instead of Jasmine? In the provided code snippet, angular.mock.dump is defined, but unfortunately angular.mock.inject remains undefined. <!DOCTYPE html> <html ng-app="mymodule"> & ...

Ways to implement a worldwide change in a subordinate entity

I am facing a challenge where I need to apply a global position, obtained from a WebVR controller, to a child object that is nested within multiple parent objects with transformations. The child object I want to update does not have any transformations app ...

Obtain the session value in React

Currently, I am working on a React app along with a server-side application. I have successfully created a user session on the server side and now I want to retrieve its value in my React app. Can someone guide me on how to achieve this? const [user,setUse ...

Angular 5: There was an issue with the property not defined for lowercase conversion

I keep encountering this error whenever I attempt to filter a column of a table. The data is retrieved from a FireStore cloud collection and the 'auteur' field is defined in each document. Here is how my component looks: import { Component, OnI ...

Tips for adjusting the font size of choices within the Material UI autocomplete component

Hey there, I'm currently working on a project using the Material Table and I'm looking to adjust the font size of the options in the Material UI Autocomplete. Any tips would be greatly appreciated! Thanks https://i.sstatic.net/ZM17w.png import R ...

Unfamiliar function detected in the updated Vue Composition API

I am currently in the process of converting a basic SFC to utilize the new Vue CompositionAPI. The original code functions perfectly: export default { data() { return { miniState: true } }, methods: { setMiniState(state) { if ...

The user removal process is not functioning properly

I'm encountering an issue in my Angularfire project while trying to remove a user. The email and password are being passed correctly, but the method responsible for user removal isn't getting executed. Below is the snippet of code from my Authent ...

The jsPDF tool captures only the visible frame in a screenshot instead of printing the entire content on the screen

Recently, I integrated the jsPDF npm module into my Angular application to convert HTML to PDF. However, I encountered an issue where printing a website page to PDF only captures a screenshot of the visible area in Firefox and Chrome, as well as in Interne ...

Can an inline try be implemented without including a catch block as demonstrated?

I have a scenario where I need to execute code that may result in an error without catching it. If the response is not valid JSON, then the desired outcome should be 0: Here is the code snippet I am considering: var overallProgress = try {JSON.parse(text ...

Interactive selection menu with jquery technology

I am in the process of developing a dynamic dropdown feature using jQuery var rooms = $("<select />"); $(res.data).each(function () { var option = $("<option />"); option.html(this.name); op ...

Guide to implementing bidirectional data binding for a particular element within a dynamic array with an automatically determined index

Imagine having a JavaScript dynamic array retrieved from a database: customers = [{'id':1, 'name':'John'},{'id':2, 'name':'Tim}, ...] Accompanied by input fields: <input type='text' na ...

What is the proper method to set up jQuery in scripts loaded via AJAX? I keep receiving the error message: 'Uncaught TypeError: Object #<Object> has no method'

I have developed a website with an index page that contains a div for loading the content of each individual page. Initially, I included external JS files and performed initializations within each separate page. However, most of the time when the page loa ...

What is the process for updating parameters before finalizing a route in Vue.js?

I have set up a route with a value parameter: routes.push({ name: 'test', path: '/test/:value', component: resolve(__dirname, 'src/pages/test.vue'), }); Now, I want to modify the route so that it also include ...

Every other attempt at an Ajax request seems to be successful

I'm new to using ajax and I'm having an issue with submitting a form through a post request. Strangely, the code I wrote only works every other time. The first time I submit the form, it goes through ajax successfully. However, on the second subm ...

Is it possible to utilize the AmMap API for developing an Android application?

I am currently in the process of creating a unique application with HTML, JS, and jQuery Mobile that will feature interactive maps. I have been searching the web for APIs to incorporate interactive maps into my project without using Google Maps APIs when I ...

Displaying all API data by default in Vue.js, even with filters, sorting, and search enabled - how can it be done?

I am currently utilizing Vue.js version 3 and below is the code snippet: <template> <h5>List of Products</h5> <h3>Filter</h3> <button v-on:click="resetOptions">Reset</button> & ...

"Troubleshooting the path problem in Vue.js router version 3.0.1

Encountered an unexpected issue and seeking assistance for resolution. Below is a snippet of the route configuration that I am currently dealing with: routes: [ { path: '/signin', name: 'signin', component: SignIn }, ...

The current version of HTML5 Context Menus is now available

I'm in need of implementing the HTML5 Context Menu feature. Currently, only Firefox supports it. My main objective is to add some menu options without replacing the existing context menu. How can I achieve the same functionality now? I am aware of va ...