Checking whether certain item is in certain array using javascript

时间:2016-04-25 09:00:31

标签: javascript arrays

I have 10 different arrays. Each array has different numbers.

array1 = [1,2,3,4,5]
array2 = [6,7,8,9,10]
...
array 10 = [51,52,53,54] 

let's say I pass in 7. Then I want to know which array it is from and want to return array number. So in this case it is going to be 2.

Should I write a switch statement for each array? Appreciate it in javascript.

4 个答案:

答案 0 :(得分:1)

try:

var arrays = [array1, array2, ..., array10];

for(var i=0; i<arrays.length; ++i) {
   if (arrays[i].indexOf(value) != -1) {
       console.log('found in array' + (i+1));
   }
}

答案 1 :(得分:0)

One way to do this is have the arrays in an object and iterate over the keys/values. This method doesn't presume the arrays (and therefore their names) are in sequential order.

Note: this will always return a the first match from the function and terminate the search.

var obj = {
  array1: [1, 2, 3, 4, 5],
  array2: [6, 7, 8, 9, 10],
  array3: [51, 52, 53, 54],
  array4: [51, 52, 53, 54, 7]
}

function finder(obj, test) {
  var keys = Object.keys(obj);
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    if (obj[key].indexOf(test) > -1) {
      return key.match(/\d+/)[0];
    }
  }
  return false;
}

finder(obj, 7); // '2'

DEMO

If you want to find all instances of a value in all arrays the function needs to be altered slightly.

function finder(obj, test) {
  var keys = Object.keys(obj);
  var out = [];
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    if (obj[key].indexOf(test) > -1) {
      out.push(key.match(/\d+/)[0]);
    }
  }
  return out;
}

finder(obj, 7); // ['2', '4']

DEMO

答案 2 :(得分:0)

You cannot directly retrieve the name of array.The reason is this variable is only storing a reference to the object.

Instead you can have a key inside the same array which represent its name. Then indexOf can be used to find the array which contain the number , & if it is so, then get the array name

var array1 = [1,2,3,4,5];
array1.name ="array1";
var array2 = [6,7,8,9,10];
array2.name ="array2";
var array10 = [51,52,53,54]
array10.name ="array10";

var parArray = [array1,array2,array10]

function _getArrayName(number){
  for(var o=0;o<parArray.length;o++){
    var _tem = parArray[o];
    if(parArray[o].indexOf(number) !==-1){
      console.log(parArray[o].name);
    }
  }

}
_getArrayName(6) //prints array2

jsfiddle

答案 3 :(得分:0)

One fast method should be using hash tables or as i would like to call LUT. Accordingly this job boils down to a single liner as follows;

var arrs = {
             arr1 : [1,2,3,4,5],
             arr2 : [6,7,8,9,10],
             arr3 : [12,14,16,17],
             arr4 : [21,23,24,25,27,20],
             arr5 : [31,34,35,39],
             arr6 : [45,46,44],
             arr7 : [58,59],
             arr8 : [66,67,69,61],
             arr9 : [72,73,75,79,71],
             arr0 : [81,85,98,99,90,80]
           },
     lut = Object.keys(arrs).reduce((p,c) => {arrs[c].forEach(n => p[n]=c); return p},{}),
  findar = n => lut[n];

document.write("<pre>" + findar(12) + "</pre>");