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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:18:43+00:00 2026-05-15T08:18:43+00:00

To begin with, I have a database that holds map data for a game

  • 0

To begin with, I have a database that holds map data for a game I am creating. A script on my page uses JSON to retrieve a certain amount of that data from the database and store it in an array.

When the data is retrieved it goes through a function that finds out how many individual tiles are used in that particular area. Here is the code:

var xmlhttp = new XMLHttpRequest;
xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4){
        var map = JSON.parse(xmlhttp.responseText);
        var mapTiles = new Array;
        for(var count = 0; count < map.length; count ++){
            if(map[count]){
                if(map[count]['tile'] in mapTiles == false){
                    mapTiles.push(map[count]['tile']);
                }
            }
        }
        alert(mapTiles);
    }
}

For each time the script finds a tile number that isn’t already in the mapTiles array it adds it to it.

Currently, the script is fetching 1024 records that all but one contain the tile value of ‘1’ the other of which contains the tile value of ‘2’. This means that when I alert the mapTiles array it should display “1, 2” but instead it displays “1, 1, 2”. So there is a slight error in the script but I cannot find it.

  • 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-15T08:18:44+00:00Added an answer on May 15, 2026 at 8:18 am

    The in operator checks to see if the object has the named property or contains an element at the array index. In your case, the check is always false because the first time, there are no elements and so it returns false. When mapTiles contains 1 it’s in the zeroth position and thus the check returns false. When you check for 2, the last element is in the first position and thus it returns false. You need to iterate through the array and check for the value of the element, rather than use the in operator.

     function contains( obj, aray )
     {
          for (var i = 0, len = aray.length, isIn = false; i < len && !isIn; ++i) {
              isIn = aray[i] === obj;
          }
          return isIn;
     }
    
    
     ...
     if(!contains(map[count]['tile'],mapFiles)){
         mapTiles.push(map[count]['tile']);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.