Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:33:47+00:00 2026-06-02T13:33:47+00:00

I have an small array of data which i want to check if my

  • 0

I have an small array of data which i want to check if my values im checking against exist in the array together.

This is how the data looks like from my console.log(astar);

https://i.stack.imgur.com/XRKlU.jpg

My attempt was:

 console.log(astar); // display array info
     for (i=0; i < 50; i++){    
        for (j=0; j < 50; j++){ 
        if( i in astar && j in astar[i] ){
               abposx = getx(i);
               abposy = gety(j);
        ctx.fillStyle = "#000";
        ctx.fillRect (abposx,abposy,10,10);             
    }   

The idea is the “inner arrays” which have [0][1] positions im trying to see if “any” of them have [0] == i and [1] == j if so = true.

How should i alter it to work correctly & most efficiently – so that it will draw when it is found in the arrays

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:33:51+00:00Added an answer on June 2, 2026 at 1:33 pm

    What you are currently doing is equivalent to

    for (i=0; i < astar.length; i++)
        for (j=0; j < astar[i].length; j++) {
            abposx = getx(i);
            abposy = gety(j);
            ctx.fillStyle = "#000";
            ctx.fillRect(abposx,abposy,10,10);
        }
    

    You may want:

    for (i=0; i < astar.length; i++) {
        if (astar[i][0] > 50 || astar[i][1] > 50)
            continue;
        abposx = getx(astar[i][0]);
        abposy = gety(astar[i][1]);
        ctx.fillStyle = "#000";
        ctx.fillRect(abposx,abposy,10,10);
    }
    

    Looping through all coordinates is very inefficient and would need a third loop for searching in astar. Better loop through astar and paint what you can get.

    The correct three-loop code would have been:

    for (i=0; i < 50; i++)
        for (j=0; j < 50; j++)
            if (astar.some(function(item) {
                return item[0] == i && item[1] == j;
            }) {
                abposx = getx(i);
                abposy = gety(j);
                ctx.fillStyle = "#000";
                ctx.fillRect(abposx,abposy,10,10);
            }
    

    (The some() loops through the array and returns true if an item matches the condition function. It equals the extended version posted by bfavaretto). Resulting in a performance of O(n^2.5)…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data: values = np.array( [0,1,2,3,4,5] ) I want to plot values as
I have a very small threaded application, which is collecting small chunks of data
I have a small program to do in Java. I have a 2D array
I have a small assignment in C. I am trying to create an array
I have an array called $times . It is a list of small numbers
I have small script in bash, which is generating graphs via gnuplot. Everything works
I have following procedure which is filling up null values in a column. The
I have a small array of structs, each struct has three fields, all strings.
I am making a small iPhone application in which I have implemented the database
I am making a special calculator which accepts user input and looks up data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.