-1

i want to check if an array containes other array in ExtJs i tried this but without success

if( [2,1] in [1,2,3]){
     console.log("true"); }

i know that i can do it manualy but is there a direct methode

5
  • Are both arrays guaranteed to be unique? Does order matter? Commented Apr 4, 2018 at 23:57
  • Possible duplicate of Javascript 2d array indexOf Commented Apr 5, 2018 at 0:01
  • no order doesn't matter Evan Commented Apr 5, 2018 at 0:42
  • i saw it it's not the same Taki Commented Apr 5, 2018 at 0:42
  • @Amor.o you should use Ext.Array utility object, have a look to my answer Commented Apr 5, 2018 at 15:39

3 Answers 3

1
var arr1=[2,1],
    arr2=[1,2,3];

if(Ext.Array.merge(arr1,arr2).length===arr2.length){
    console.log('HELLO!');
}

If you're using Extjs, using this method you can win without functions and cicles

Simply merging the arrays.

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

Comments

0

You need to loop over the Array to check if the values are in it:

function inArray(v, a){
  for(var i=0,l=a.length; i<l; i++){
    if(a[i] === v){
      return true;
    }
  }
  return false;
}
function allInArray(t, a){
  for(var i=0,l=t.length; i<l; i++){
    if(!inArray(t[i], a)){
      return false;
    }
  }
  return true;
}
var yourArray = [1, 2, 3], tests = [2, 1];
console.log(allInArray(tests, yourArray));
console.log(allInArray([2, 4, 1], yourArray));

// another way
function allIn(t, a){
  return t.length === t.map(x => +a.includes(x)).reduce((n, v) => n+v);
}
console.log(allIn(tests, yourArray));
console.log(allIn([3, 4], yourArray));

1 Comment

thank u i already have a solution like that but i ask if there is a direct solution , that's means with a predefined function
0

In extjs you have Ext.each function to loop out through the array

var array_1 = [2,1];
var array_2 = [1,2,3];
Ext.each(array_1,function(value_of_arr1){
    if(array_2.indexOf(value_of_arr1) == -1){
        console.log('array_1 value doesnt exist');
        return;
    }else{
        console.log('array_1 value does exist');
        return;
    }
});

In above code the -1 is returned from the if condition if the value of array_1 is not found in the array_2

if value is found the array index in which the value resides is returned

Comments

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.