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 7661309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:34:51+00:00 2026-05-31T13:34:51+00:00

I have an array in JS and need to look up the data by

  • 0

I have an array in JS and need to look up the data by doing checks against it, but am wondering how i do it =/

My array is structured like this :

//global array spritea
var listObj = {
    id: uid,
    data: [
        abposx,
        abposy,
        (abposx+imgwidth),
        (abposy-imgheight)
    ]
};                  

spritea.push(listObj);

This is in a loop so an example of the array is :

spritea = [
    {
        id: "135", 
        data: [9,129,345, 687]
    },
    {
        id: "239", 
        data: [596,382,0,687,33467]
    }
];

So what im trying to do is this:

Find

id where x is > data[0] && y > data[1] && x < data[2] && y < data[3]

And then it returns the id ?

Any one know how to do that? Its really confusing me =/

  • 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-05-31T13:34:52+00:00Added an answer on May 31, 2026 at 1:34 pm

    It’s going to be inefficient for large arrays since it’s O(n) (simple linear search), but should do the trick:

    spritea = [
        {
            id: "135", 
            data: [9,129,345, 687]
        },
        {
            id: "239", 
            data: [596,382,0,687,33467]
        }
    ];
    
    function searchSprites(sprites, x, y) {
        var matches = [],
            i = 0,
            data = null;
        for (i = 0; i < sprites.length; ++i) {
            data = sprites[i].data;
            if (x > data[0] && y > data[1] && x < data[2] && y < data[3]) {
                matches.push(sprites[i].id);
            }
        }
        return matches;
    }
    ​
    

    It’s untested, but should work. Also, if you need better performance, you’re going to have to index the data somehow.

    Edit: jsfiddle: http://jsfiddle.net/mP6cH/3/

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

Sidebar

Related Questions

I have an array like this: object[] args and need to insert those args
EDIT: this is my full code: i have a array how look like this:
I have 2 arrays in PHP that look something like this: $rows = array(11,12,14,14,11,13,12,11);
I have an array which looks like this $dataArray = array ( 0 =>
I have an array of characters, which may look like: 1 0 0 1
I have an array of objects called objarray . Each object looks like this:
I have an array of folders/files that looks like this: Array ( [Root Folder
I have data that look like: COVUP.0.db2inst1.NODE0000.CATN0000.20110304144237.001 DBB.0.db2inst1.NODE0000.CATN0000.20100730102440.001 DBB.0.db2inst1.NODE0000.CATN0000.20100809104005.001 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125123615.001 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101125153555.001 DOMINIC1.0.db2inst1.NODE0000.CATN0000.20101126162500.001 I need
I currently have this code: $dates = array(); foreach($data['sc_event_dates'] as $date) { if($date >
ok so i have this array and i need to loop through <?php foreach($classes

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.