1

Hi everybody and thanks for your helps.

hi have a context here in a game engine, i would like know if existe a easy way to return each array ? Ex: [true,true,false,false] here the snippet i make it more simple and clean to read , but is very similar to what i need to do.

 Gate1 = [];
Gate1 = [[3,4],[4,6]]; // [[ItemID,NbItem],[ItemID,NbItem]]

function check(ID, NEED, GVariable) {
for (var F=0; F<ID.length; F++) {
var Founds = [ID[F],NEED[F]];
if (Founds === GVariable[F]){
//return stop the loop, but i need to test all ID.length and return true or false in array it in array
		return true;
		}
		else {return false;}
	}
}
var Get = check([3,4,10,11],[4,6,8,7],Gate1);
console.log(Get); //need to be ===  Get[[true,true,false,false]] 
alert("Gets= "+Get+'\n'+'But i want = [true,true,false,false]');

2
  • and what exactly is the logic, to decide whether it is true value of false in array? Commented Dec 18, 2016 at 9:01
  • example for test if the array GateX have all item recipe to build ex: the gate. If one false, cant not build, and is need to show why and where in the array Commented Dec 18, 2016 at 9:12

3 Answers 3

1

You could use a hash table and check against the value you need. Then return with Array#map an array with the result.

I strongly recommend to rename the variables to a more common style with small letters.

function check(ID, NEED, GVariable) {
    var hash = Object.create(null);

    GVariable.forEach(function (a) {
        hash[a.join('|')] = true;
    });
    return ID.map(function (id, i) {
        return hash[[id, NEED[i]].join('|')] || false;
    });
}

var Gate1 = [[3, 4], [4, 6]], // [[ItemID,NbItem],[ItemID,NbItem]],
    Get = check([3, 4, 10, 11], [4, 6, 8, 7], Gate1);

console.log(Get); // [true, true, false, false]
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

4 Comments

OMG NICE NICE , I am at the beginning, I do not know all the JavaScript trick. Your code are very clean. I will study more the map function. Thank you so much friend. For my variable names, I usually write in lowercase or uppercase according to their importance and their SCOPE. Because some var are for a for a specific .js file, ans other need to use for all .js files. I indeed exaggerate for this example. Thank you so much for your help, you are great.
These possible to make this function functional, but with arrays in dynamic position. var Gate1 = [[4, 6],[3, 4]] // Also Give [true, true, false, false]
the order does not matter. do you need the same order?
Sorry Yes, I had to make a bad entry. Is true, the order does not matter. Yes for a version with the mandatory order. I will be curious to study the difference between the 2 codes. This will be nice to you.
1

You could also zip your ID and NEED arrays and use the every function to compare zipped and GVariable arrays:

function checkit(ID, NEED, GVariable) {
  var result = [], arrays = [ID, NEED];
  zipped = arrays[0].map(function(_,i){ 
      return arrays.map(function(array){return array[i]})
  });
  zipped.forEach(function(arr, idx) { 
    result.push(arr.every(function(elt, i) {
      return (elt == GVariable[i][idx]);
    }))
  })
  return result;
}

var Gate1 = [[3,4],[4,6]]; 
var Get = checkit([3,4,10,11],[4,6,8,7],Gate1);
console.log(Get); 

1 Comment

Thank you for sharing this friend. It's a very interesting way.
0

Try this...Use JSON.stringify()

Gate1 = [];
Gate1 = [[3,4],[4,6]]; // [[ItemID,NbItem],[ItemID,NbItem]]

function check(ID, NEED, GVariable) {
for (var F=0; F<ID.length; F++) {
var Founds = [ID[F],NEED[F]];
if (JSON.stringify(Founds) == JSON.stringify(GVariable[F])){
//return stop the loop, but i need to test all ID.length and return true or false in array it in array
        return true;
        }
        else {return false;}
    }
}
var Get = check([3,4,10,11],[4,6,8,7],Gate1);
console.log(Get); //need to be ===  Get[[true,true,false,false]] 
alert("Gets= "+Get+'\n'+'But i want = [true,true,false,false]');

1 Comment

JSON.stringify, nice found i will study this , is exact what i need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.