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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:28:19+00:00 2026-05-28T03:28:19+00:00

I am making an javascript game, featuring several levels, stored in json. When loading,

  • 0

I am making an javascript game, featuring several levels, stored in json.
When loading, I run a function to “parse” the level object by replacing values that are not present by their default values, or by replacing some values by objects, such as:

//i is the variable I use to loop through the enemies

if (typeof(level.enemies[i].life)=="undefined") {
 level.enemies[i].life=100;
}

if (typeof(level.enemies[i].follow)=="number") {
 level.enemies[i].follow=level.enemies[level.enemies[i].follow];
 // replace the number of the enemy,
 // by a reference to the actual enemy.
}

The problem is that I have a lot of “ifs” similar to these throughout the function, and I am wondering if I somehow can reduce them to a function so I can do this:

replaceByType(level.enemies[i].life,"undefined",100);
replaceByType(level.enemies[i].follow,"number",level.enemies[level.enemies[i].follow]);

Sadly I don’t know how to do this, because their is no way to pass a variable(other than an object) by reference. Maybe there is another way to simplify my code?

  • 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-28T03:28:20+00:00Added an answer on May 28, 2026 at 3:28 am

    Since you can pass objects by reference, you can pass a property of the object that you want to replace.

    function replaceByType(obj, property, type, value) {
       if(typeof obj[property] === type) {
         obj[property] = value;
       }
    }
    

    Which you can call almost how you were wanting to:

    replaceByType(level.enemies[i], "life", "undefined", 100); 
    replaceByType(level.enemies[i], "follow", "number", level.enemies[level.enemies[i].follow]); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a 2D game in javascript/ajax, that will be using data stored
I'm making a javascript game and need to bind a lot of keys to
I'm making a game using canvas, and javascript. When the page is longer than
I am making a Javascript game with the canvas tag, and I am using
I am making a simple PHP and JavaScript multiplayer game. Here is a quick
I'm making a game using javascript + canvas. I use the code below to
I am making a browser based javascript game. As such, within the game there
I'm making a game in HTML5's Canvas + JavaScript. My CPU use for the
I am working on making a simple Javascript game and need to be able
I'm making a spot-the-difference game in HTML, CSS + Javascript, but one of the

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.