What is the method for obtaining multiple indices on an Array?

Attempting to find multiple index positions in an Array for a Boolean value.

Experimenting with while and for loops to iterate through more than one index position without success so far.

Below is the code snippet:

let jo = [1,2,3,4,5]
let ji = [1,2,3]

let checker = (arr1,arr2) => {

  let falsy = arr1.every(num => arr2.includes(num)) == false ? 
    arr1.map(falsy => arr2.includes(falsy)) : "tba";

  //the block below is the frustrated attempt:

  let i = falsy.indexOf(false);
  while(i>=0){
    return falsy.findIndex(ih => ih == false)
  }

}

console.log(checker(jo,ji))

Desiring to store the index at which false occurs after iterating over the entire array. This will enable returning only the false values from falsy like this:

return falsy[i] = [4,5]

Subsequent enhancements will be made to check both arr1 x arr2 or arr2 x arr1 in the initial if statement.

Thank you in advance!

Answer №1

It appears that you are trying to find the variance between two arrays. This scenario often calls for using Sets. Below is an example of how your code could be structured:

let array1 = [1,2,3,4,5]
let array2 = [1,2,3]

const findDifference = (arr1, arr2) => {
    return new Set(arr1.filter(x => !new Set(arr2).has(x)))
}

console.log(findDifference(array1, array2));  // {4, 5}

If you wish to determine the indices of the variances, you would need to utilize a map function on the result of the new Set as shown below:

const findDifference = (arr1, arr2) => {
    const differenceSet = new Set(arr1.filter(x => !new Set(arr2).has(x)));
    return Array.from(differenceSet).map(value => arr1.indexOf(v));
}

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

Incrementally add a new object to an existing array of objects

I have an array of objects below. When I do a console.log, this is what I see: [Object, Object, Object] 0:Object name: "Rick" Contact: "Yes" 1:Object name:"Anjie" Contact:"No" 2:Object name:"dillan" Contact:"Maybe" Now, I wa ...

How can you flip an array without compromising the unique keys within?

Currently, I am working on setting user permissions which are stored as an array in the database. The output from my html form is as follows: { "where_to_buy": "true", "spec_sheet": "true", "dwg_access": "true", "bim_access": "true", "product_prices": "tr ...

What is the best method for creating thumbnail URLs for video files in HTML with JavaScript?

I am facing an issue with generating a thumburl for uploaded video files. After dynamically creating an input file and uploading local video files, I am able to retrieve the name and size of the file along with other information as shown in the screenshot ...

Creating a custom jQuery plugin for exporting data

I am crossing my fingers that this question doesn't get marked as 'already answered' because I have thoroughly searched previous questions and unfortunately, my specific case is not listed anywhere. I have successfully created a jQuery func ...

Find the closest point within a 2D numpy array

Despite reading numerous posts on finding the closest value in a numpy array or the closest coordinate in a 2D array, none of them address my specific issue. The challenge I'm facing involves having a 2D numpy array structured like this: [[77.6288173 ...

Exploring Angular 9: Harnessing the Power of Fork Join with an Array of

I have a challenge where I need to send multiple API requests to an endpoint by iterating over an array of values To handle this, I decided to use rxjs library and specifically the forkJoin method //array to keep observables propOb: Observable<any>[ ...

Tips for entering multiple values in an input field

I am attempting to implement a feature where multiple names can be added from an autocomplete drop-down menu to an input field, similar to the example shown below: Here is what I aim to create: Upon selecting an item from the drop-down, it should appear ...

personalized link when uploading images in Jodit Editor

I recently integrated the Jodit Editor (react) with the Insert Image option, allowing users to upload images that are saved in the default location set by the Editor. Now I am curious about how to use a custom URL to insert an image in the editor. Here i ...

Exporting modules for testing within a route or controller function

I'm relatively new to NodeJS and the concept of unit testing. Currently, I am using Jest, although the issue seems to persist with Mocha, Ava, or any other test framework. It appears that my problem revolves around the usage of export/import. In one ...

Trouble arises when trying to import Jest with Typescript due to SyntaxError: Import statement cannot be used outside a module

Whenever I execute Jest tests using Typescript, I encounter a SyntaxError while importing an external TS file from a node_modules library: SyntaxError: Cannot use import statement outside a module I'm positive that there is a configuration missing, b ...

Trouble with the Ngx-Captcha feature

I am currently utilizing https://www.npmjs.com/package/ngx-captcha/v/11.0.0. <ngx-recaptcha2 #captchaElem [siteKey]="'6Leh1ZIjAAAAAG8g0BuncTRT-VMjh3Y7HblZ9XSZ'" (success)="handleSuccess($event)" [useGlobalDomain]="fals ...

Alter the div's HTML content once the Ajax operation is completed

I have a div element that looks like this: <div class="progress" id="progress-bar"></div> I am using the following JavaScript code along with an ajax call to retrieve some data. The data returned is 0, however, the content is not being added ...

Ways to smoothly conclude a loading animation in real-time

I have a unique challenge of creating a loading animation using Adobe After Effects instead of CSS. The main goal is to develop an animation that continuously loops, but when the loading process stops, it ends with a distinct finishing touch. For instance, ...

Omitting specific modules from the Webpack DLL compilation

I have a universal JavaScript application built with Webpack. I've utilized the DLL plugin to pre-build all my node_modules, but recently encountered an issue when adding a new library. This specific library is causing the DLL build process to fail (d ...

What improvements can be made to optimize this SQL query and eliminate the need for an additional AND statement at the end

I am working on dynamically constructing a SQL query, such as: "SELECT * FROM TABLE WHERE A = B AND C = D AND E = F" Is there a more efficient way to construct this SQL query without adding an extra AND at the end? Here is my current code snippet: le ...

Is it possible for Cypress to execute test files that are imported from outside of the Cypress folder

Currently, I am developing an E2E test framework using Cypress and encountered an issue while trying to import spec files from locations outside the traditional Cypress directory structure (which includes directories for fixtures, integration, plugins, and ...

"Unleashing the Glamour: Tips for Decorating an Amazing Ajax Pop

I need help styling a magnific pop-up that displays content from a uri. The content is just plain text and I want to avoid using any html code as the popup will be triggered by a button. The current code I have written functions correctly, but the appeara ...

Using a numeral within a Font Awesome icon symbol to customize a label for a Google Maps marker

I have a Google Maps marker displayed below, applying a Font Awesome icon as the label/icon. However, I am unsure how to a.) change the color of the marker and b.) include a number inside the marker. Referencing this Stack Overflow post This is the code ...

Setting up a web server with a cyclical challenge

I've encountered an issue while hosting my server.js file with the configured API on Cyclic. The deployment was successful, but every endpoint call is returning a status 500 error. Additionally, I have hosted the React front-end on github pages. I&apo ...

Aggregate array based on specified criteria in ReactJS

Let's consider the following array data: array = [ { id: 1, count: 0.5 cost: 100 user: {id: 1, name: "John 1"}, type: {id: 1, name: "T1"}, period: {id: 1, name: "2021"} ...