Finding the smallest index in an array without considering the value of the element

Consider the following array:

array = [[3, 3], [3, 4], [3, 5], [3, 6]]

Now let's take a look at this conditional statement:

array = [[3, 3], [3, 4], [3, 5], [3, 6]]

for(let i = 0; i < array.length; i++){
    if (array[i][0] === 3 && array[i][1] === 4 || array[i][0] === 3 && array[i][1] === 5){
        console.log(i)
    }
}

In this scenario, the indexes that would be printed are 1 and 2, showing up sequentially in the console.log().

Your challenge is to extract the minimum index (1 in this case) from within the loop. It isn't possible to store these numbers in an array due to certain constraints, so the minimum must be determined during iteration without prior knowledge of which element will have the smallest index.

Do you think this task can be accomplished or is it a case of overthinking the problem?

Answer №1

To efficiently find a specific element in an array, you can declare a variable outside the loop to store the index when the condition is met:

arr = [[3, 3], [3, 4], [3, 5], [3, 6]]

var ind = 0

for(let i = 0; i < arr.length; i++){
    if (arr[i][0] === 3 && arr[i][1] === 4 || arr[i][0] === 3 && arr[i][1] === 5){
        ind = i;
        break;
    }
}

console.log(ind)

Another approach is to use the Array.findIndex method for a more concise solution:

arr = [[3, 3], [3, 4], [3, 5], [3, 6]]

var ind = arr.findIndex(e => e[0] === 3 && e[1] === 4 || e[0] === 3 && e[1] === 5)

console.log(ind)

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

I encountered an issue while attempting to utilize a JavaScript script - I received an error message stating "Uncaught ReferenceError: jQuery is not defined."

Currently, I am utilizing a template with multiple pages and I need to use one of them. I followed all the necessary steps correctly, but I encountered an error in the console that says Uncaught ReferenceError: jQuery is not defined. Below is my HTML scrip ...

What is the process for redirecting to a Courier site using the consignment number of the item?

Currently, I am working on an e-commerce project that involves redirecting customers to a specific courier site where they can track their shipments using the consignment number. My goal is to make this process seamless for the user so they don't have ...

Strategies for correctly parsing a file and storing the data from each struct in an array

To accomplish my task, I will be reading in a file where each struct-sized amount of characters will be assigned to parts of a struct array. For example, the first thirty characters will be assigned as the first name, next thirty as the last name, and the ...

The embedded video from YouTube refuses to appear behind other elements

I am facing an issue with YouTube embedded videos on my website. The problem is that these videos do not follow the z-index rules and are always displayed above all other elements on the page. I have tried: $('iframe','.video_container&apos ...

What is the purpose of these specific Javascript expressions (+!)?

Currently, I am attempting to convert JavaScript code into Python. One of the challenges I am facing is understanding the purpose of certain expressions, which has left me feeling stuck. Below is the segment of code that I am trying to translate. va ...

Combining numerous draggable and droppable functionalities onto a single element

I attempted to include two draggable stop event handlers in a paragraph. However, only the second one seems to fire. How can I ensure that both of them trigger? <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jq ...

Problem with AngularJS Multiselect checkbox dropdown configuration

In my application, I have a pop-up that includes a multi-select dropdown menu. Here is the code for the Multi-Select Dropdown: <select name="edit_tags" class="form-control" id="advisor_article_tagsx" multiple="" required ng-model="article_selected ...

Building a single-page app optimized for mobile viewing with Foundation framework

Currently facing an issue with the scaling of the viewport on certain mobile devices when loading new content via ng-include in our responsive website built on Foundation. The problem arises as the width of the page breaks, leading to horizontal scrolling. ...

Passing selection from child to parent in ReactJS

When it comes to passing data from parent components to child components, using props is a common practice. But how can data be sent back up to the parent component? I'm working on a set of dropdown menus where users can make selections: DropdownMen ...

Is it possible to rewrite this function recursively for a more polished outcome?

The function match assigns a true or false value to an attribute (collapsed) based on the value of a string: function match(children) { var data = $scope.treeData for (var i = 0; i < data.length; i++) { var s = data[i] for (var ...

What is the best way to manage the maximum number of concurrent AJAX requests?

I am currently working on an autocomplete search bar feature. <input type="text" class="form-control" id="text" > <script> $("input").keyup(function(){ let key = $("input").val(); ...

The load event in React's iframe is failing to fire after the src attribute is modified using state

In the process of creating a registration form for a React application, we encountered the need to incorporate an HTML legal agreement as an iframe. This legal document is available in various languages, one of which can be selected using a drop-down menu; ...

Cross domain request in a simple HTML document

I'm currently working on an app that is strictly in plain HTML files without a server. I'm facing difficulties with cross domain requests from JavaScript. Whenever I try to make a request, the browser displays this error message: XMLHttpRequest ...

Executing complex queries in mongoose using the $or operator

I'm in search of an efficient way to create clean code for executing multiple complex queries. Within my MongoDB database, I have two collections: followers and events. The first query involves retrieving all followers associated with a specific use ...

What is the proper way to retrieve the Nuxt context within the fetch() hook?

Is there a way to access the props within an async fetch() function when also using async fetch(context)? I'm trying to figure out how to work with both simultaneously. ...

Leveraging JavaScript to determine whether a number is even or exiting by pressing the letter "q"

The main goal is to have the user input a number to check if it is even, or enter 'q' to exit the program. var readlineSync = require('readline-sync'); var i = 0; while (i <= 3) { var num = readlineSync.question ...

Struggling to pass a string as a parameter for a class in C++, but it is erroneously interpreted as an array of characters

Encountering an issue with initializing Warrior Objects in my main function Below is the code for my Warrior Class: class Warrior{ public: Warrior(const string& input_name, int& input_strength) :name(input_name), strength(input_strength) {}; st ...

Is it possible to update the state of an array within a .then() function in React?

` After retrieving an array of points with city and state information, I attempted to convert these points to latitude and longitude by making a fetch call. However, upon trying to update the state of the newly generated array, I found that it remained ...

When the page loads, capture a PHP variable and transfer it to a different page using AJAX

On page1, I need to send these variables to page2 using the post method. Despite my repeated attempts, I am finding it difficult to achieve as intended. calls(); function calls(){ function calls(){ var l="<?php echo $abc ; ?>"; var u="<?php ec ...

Ajax is invoked only one time

The system is set up to display a follow button and an unfollow button. Clicking the follow button should make the unfollow button appear, and vice versa. Currently, when you click "follow", the database updates and the follow button disappears while the ...