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

Developing a table with JavaScript by parsing JSON data

Starting off, I am relatively new to working with JavaScript. Recently, I attempted to generate a table using data from a JSON file. After researching and following some tutorials, I successfully displayed the table on a web browser. However, I noticed tha ...

Unhandled error: 'this' is not defined within the subclass

My code snippet is causing an issue when run in Node v4.2.4: "use strict"; class Node { constructor() { } } class Person extends Node { constructor() { } } const fred = new Person(); Upon running the code, I encounter the following error mes ...

The screen is cloaked in a dark veil, rendering it completely inaccessible with no clickable

Utilizing Bootstraps modals, here is my current layout. Within the site's header, there exists a "settings" button that triggers a modal containing various options. These options are not tied to the question at hand. The button responsible for displ ...

Designing Checkboxes and Radio Buttons

I am seeking a way to customize the appearance of checked and unchecked checkboxes using images without modifying the HTML structure. I would prefer not to add labels, classes, or IDs to the elements. The provided example only works in Webkit browsers, s ...

Encountering the "Local resource can't be loaded" error when attempting to link a MediaSource object as the content for an HTML5 video tag

I'm attempting to make this specific example function properly. Everything runs smoothly when I click the link, but I encounter an error when trying to download the HTML file onto my local machine and repeat the process. An error message pops up sayi ...

Enlarge the div with a click

I was looking for a solution on how to make a div expand when clicked using jQuery I came across a tutorial that seemed simple and perfect, but I couldn't get it to work when I tried to replicate the code. Do you know if this code is still valid wit ...

Exploring the Power of GraphQL Args in Mutation Operations

Currently, I am in the process of developing a blog service using express and apollo-express in conjunction with mongodb (mongoose). While implementing mutation queries, I have encountered difficulties in accessing the arguments of a mutation query. I am ...

Having trouble locating node_modules/nan on your system?

Trying to globally install this project is proving to be a challenge as I run the command npm i -g. Followed these steps: git clone https://github.com/superflycss/cli cd cli npm i npm i -g However, encountered this result: ole@mki:~/cli$ npm i -g npm W ...

Encountered an Angular SSR error stating "ReferenceError: Swiper is not defined"

When attempting to implement SSR (Server-Side Rendering) in a new project, everything runs smoothly and without issue. However, encountering an error arises when trying to integrate SSR into an existing project. ...

Use a for loop to fill an array with values and then showcase its contents

I have been trying to figure out how to populate an array and display it immediately when targeting the route in my NodeJS project. Currently, I am able to console log a list of objects. However, I want to populate an array and show it when accessing loca ...

Eliminating the glow effect, border, and both vertical and horizontal scrollbars from a textarea

Dealing with the textarea element has been a struggle for me. Despite adding decorations, I am still facing issues with it. The glow and border just won't disappear, which is quite frustrating. Could it be because of the form-control class? When I rem ...

What is causing the chat-widget to display a null value for the style read property?

Could someone assist me with hiding the Widget-chat? I keep getting an error that the property of style is null. Any help would be greatly appreciated. Thank you in advance. document.getElementById("chat-widget").style.display='none'; ...

Tips for adding an item to an array within a Map using functional programming in TypeScript/JavaScript

As I embark on my transition from object-oriented programming to functional programming in TypeScript, I am encountering challenges. I am trying to convert imperative TypeScript code into a more functional style, but I'm struggling with the following ...

Issue with v-model not connecting to app.js in Laravel and Vue.js framework

Snippet from app.js const app = new Vue({ el: '#app', router, data:{ banana:'' } }); Code found in master.blade.php <div class="wrapper" id="app"> <router-view></router-view> //using Vue ...

The function webpack.validateSchema does not exist

Out of the blue, Webpack has thrown this error: Error: webpack.validateSchema is not defined Everything was running smoothly on Friday, but today it's not working. No new changes have been made to the master branch since Friday. Tried pruning NPM ...

Best Practices for Installing Webpack in a Client/Server Folder Structure

Working on a React Nodejs web application and in the process of figuring out how to bundle the frontend using webpack. This is how my project's structured: Where exactly do I need to install webpack and configure webpack.config.js? I've noticed ...

What are some ways we can enhance Map.get for react-router using ES6 Maps?

I recently implemented a map using new Map() to store my RR4 configuration within my application. My goal is to retrieve the values associated with /countries/:id when accessing /countries/1. routesMap.get('/countries/1') // should provide the ...

The appearance of the circle in Safari is rough and lacks smoothness

My circle animation works perfectly on Google Chrome, but when I switch to Safari the edges appear faded and blurry. I tried adding "webkit" to fix it, but had no luck. Is there a compatibility issue with Safari and CSS animations? Here is my code: Snapsh ...

Cross-origin request headers not permitted by the Access-Control-Allow-Headers policy in NodeJS and ExpressJS

I am currently facing an issue with my NodeJS + ExpressJS client-server setup while making API requests to a backend server. Every time I make an API request, I receive the following error: Request header field firstname is not allowed by Access-Control-A ...

Title Tooltip Capitalization in Vue.js: A Guide

Is there a way to change only the first letter of the title tooltip (from a span) to be capitalized? I attempted using CSS with text-transform: capitalize;, however, it didn't have the desired effect. <template lang="pug"> .cell-au ...