1

I'm pretty new here but i'm posting this cause i haven't found a single answer on the internet to this question.

How can I use multiple arrays as conditions to an if statement. the reason i would need this is simply for creating a 2D game. But i'm learning that even a simple 2D game has tons of variables because of all the objects involved. But here is a simple example for what I've started with.

var a = 27;
var test = 0;

if(a in {18:1, 27:1, 36:1}) {
    test = 1;
}

This tests an array of variables against one variable. I've found that this returns true but this is only half the battle.

The only place I've found any close reference to this is here.

How to shorten my conditional statements

Now the hard part is getting two arrays as conditions instead of just a variable and an array. So basically i need this idea made shorter.

var a = 27;
var b = 27;  
var c = 50;
var test = 0;

if(a in {18:1, 27:1, 36:1} || b in {18:1, 27:1, 36:1} || c in {18:1, 27:1, 36:1}) {  
    test = 1;
}

even though i'm a noob my bible is the hacker's standard:P. Which basically means i think that when i'm creating something with the process of doing something over and over without very good reason "IT IS THE DEVIL"(kudos to whoever got the references). So let me explain this again but very specific so there's no confusion. Say i create a lot of NPC(non player character) and i want a system that can detect if the individual NPC has been in contact by lets say a projectile. i want that individual to vanish and give a point to a score board. well creating functions for such characters requires a LOT of if statements. So ideally i want an if statement that somehow uses 2 or more arrays for it's conditions but look almost as short as using two variables.

maybe something that looks like this.

var test = 0;
var a = [5,6,8];
var b = [10,30,8];

if(a in b){
    test = 1;
}

NOTE: I've actually already tried this but it only took the index of b and not the numbers inside. I believe this topic deserves attention unless there's already someone out there that posted a solution(in which case it NEEDS to be advertised).

EDIT: After a long while i've come to realize that the proper(more efficient and readable) solution is to use both OOP and game engine design. I was just too young to understand how to work with data. So to anyone who see's this wondering the same thing should simply try to more thoroughly study array and class logic. In honesty javascript is NOT the place to learn this. I recommending taking a trip to processing.org. and learning the ways of using classes. if Your having trouble there you can try openFrameworks and learn OOP in c++. But the biggest part is understanding proper array mechanics. The OOP just makes it easier.

Community
  • 1
  • 1
user3440251
  • 33
  • 2
  • 8
  • hello you can merge all array in one and can find – Yadav Chetan Mar 20 '14 at 03:44
  • 2
    If you want to find contents in an array, you use `.indexOf()` on the array. If you want to search multiple arrays, well you have to search multiple arrays with separate operations. Beyond this, I'm not sure what you're really asking. You could concat all your arrays together and then use one `.indexOf()` on the result I guess. As you already seem to know, Objects are much better for lookup. Arrays are better for indexed or sequential access. – jfriend00 Mar 20 '14 at 03:46
  • In your second example, you would assign `{18:1, 27:1, 36:1}` to a variable and do `a in d || b in d || c in d`. Or you would do `[a, b, c].some(function(x) { return x in d; })`. DRY is an important concept, but it's also important to start coding and not overthink it. – Felix Kling Mar 20 '14 at 03:48

1 Answers1

2
var test = false;
var a = [5, 6, 8];
var b = { 10:1, 30:1, 8:1 };
for (var i = 0; i < a.length; i++) {
    if (a[i] in b) {
        test = true;
        break;
    }
}

If you're using a library like jQuery or Underscore.js, they have convenience functions like $.any() that can be used to replace the loop. You can also use the built-in Array#some method, but it's not compatible with IE8. Ex:

return a.some(function(x) {
    return x in b;
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Or native [`Array#some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some). – Felix Kling Mar 20 '14 at 03:50
  • from what I've heard they (underscore js, jquery, and array#some) deal with checking one array as apposed to comparing two arrays at the same time(unless i missed something(which there's no shame in that)). let me give another short example to see if this helps any. – user3440251 Mar 20 '14 at 08:01
  • instead of: var a = 1; var b = 3; var c = 5; var x[2,1,6]; if(a in{2:1,1:1,6:1}||b in{2:1,1:1,6:1}||c in{2:1,1:1,6:1}){ //excute code } It would be easier to have something check both at the same time. A made up example would be this. var y[1,3,5]; var x[2,1,6]; if(A.array = B.array){//if anything in array a would be would equal an element in array b //excute code } And the x array in the first example wasn't needed it was just there for a perspective view. sorry about the code looking slopy i'm trying to figure out how to use the comments – user3440251 Mar 20 '14 at 08:20
  • and if no such thing exists then it would be a huge improvement for someone to make a library to make this very common situation much easier. an example of when i would need this is when i'm having to check the surface of a shape against tons of other possible variables. and each shape has lots of variables of there own. so i'll have lots of long arrays to add in if statements. and honestly it's extremely hard to debug code for a game when it's hard to keep track of the hundreds of variables unless they are organized in to arrays symbolizing the surface of the shapes. – user3440251 Mar 20 '14 at 08:31
  • although i'm not good at object oriented programming and maybe because of that i'm missing something that would make this situation simple lol. i hate repeating tasks though cause it takes away the creativity of what i'm doing. i'm sure it's the same for most folks. thanks for trying to help me out though and any further ideas, suggestions, or plain blunt facts about this that i might not know about are vary welcome. – user3440251 Mar 20 '14 at 08:35
  • You're right, `array#some` just deals with checking one array. It just replaces the loop in my answer, it doesn't do everything in one step. I've shown an example of how to use it for this problem. – Barmar Mar 20 '14 at 08:48
  • well thanks for all the help folks i am grateful. although now that i'm getting more into object oriented programming i'm starting to see that it's not a necessity. and the tools you guys mentioned do work enough but it would still be nice to have a library that could accomplish this (multiple array condition) to save some time and coding. i suppose i should program my own library for this task. if i get one made or find one i'll be happy to post it here:) – user3440251 Mar 23 '14 at 09:06