简体   繁体   English

如何在三个数组之一中查找元素,然后返回父数组名称?

[英]how to find an element in one of three array and then return the parent array name?

I am trying to check if an element exists in any one of three arrays. 我正在尝试检查元素是否存在于三个数组中的任何一个中。 I don't know how to return the name of the array where the element was found. 我不知道如何返回找到元素的数组的名称。 Can anyone direct me into the right direction please. 任何人都可以将我引导到正确的方向。

I have coded a function which takes the element in search as its argument and then returns the array name: 我已经编写了一个函数,该函数将搜索中的元素作为其参数,然后返回数组名称:

 var arr1 = ['a','b','c','d'];
 var arr2 = ['e','f','g','h'];
 var arr3 = ['i','j','k','l'];     

 function chkElem(elem)
 {
    var id = elem;
    var isFound = null;

    if(arr1.indexOf(id) || (arr2.indexOf(id) || (arr3.indexOf(id))))
    {
        isFound = ????      
    }
    return isFound;     
 }

I am uncertain how to assign the parent array name to 'isFound' variable. 我不确定如何将父数组名称分配给'isFound'变量。

Thanks. 谢谢。

You should never use "variable names " in your function logic. 您永远不要在函数逻辑中使用“变量 ”。 Instead, make the arrays properties of an object and return the property name: 而是使对象具有数组属性,并返回属性名称:

var arrays = {
    "arr1": ['a','b','c','d'],
    "arr2": ['e','f','g','h'],
    "arr3": ['i','j','k','l']
};
for (var name in arrays)
    if (arrays[name].indexOf(id) > -1)
        return name;
return null;

Or, even better, use an array of arrays to search in and return the index: 或者,甚至更好的是,使用数组数组搜索并返回索引:

var arrays = [
    ['a','b','c','d'],
    ['e','f','g','h'],
    ['i','j','k','l']
];
for (var i=0; i<arrays.length; i++)
    if (arrays[i].indexOf(id) > -1)
        return i;
return -1;

Test one-by-one: 一对一测试:

if (arr1.indexOf(id) > -1) {
  isFound = arr1;
} else if (arr2.indexOf(id) > -1) {
  isFound = arr2;
} else if (arr3.indexOf(id) > -1) {
  isFound = arr3;
}

Alternatively, create a multi-dimensional array: 或者,创建一个多维数组:

var arr = [
 ['a','b','c','d'],
 ['e','f','g','h'],
 ['i','j','k','l']
];

var isFound = null;

for (var i = 0; i < arr.length; i++) {
  if (arr[i].indexOf(elem) > -1) {
    isFound = arr[i];
    break;
  }
}

Firstly, be careful of the indexOf() trap - if it fails to find the requested string, it will return -1 - which is a truthy - so you need to check explicitly like so: 首先,请小心indexOf()陷阱-如果未能找到请求的字符串,它将返回-1-这是一个事实-因此,您需要像这样进行显式检查:

if (arr1.indexOf(id) != -1)

not

if (arr1.indexOf(id))

The truthy/falsy concept also means that, if your string is the first element in the array, and so indexOf() returns false, that is a falsy, and so your condition will actually fail even though a match was made! 真实/虚假概念还意味着,如果您的字符串是数组中的第一个元素,并且indexOf()返回false,则表示虚假,因此即使进行了匹配,您的条件实际上也会失败!

Secondly, you cannot return the name of the array - or, to be more precise, the name of the variable that references it in the JS memory. 其次,您不能返回数组的名称-更确切地说,不能返回在JS内存中引用该数组的变量的名称。 You can either: 您可以:

1) return the array itself 1)返回数组本身

if (arr1.indexOf(id) != -1) return arr1;

2) store your arrays in a central object and return the name of the property that you found it in 2)将数组存储在中央对象中,并返回在其中找到它的属性的名称

var arrs = {
    'one': ['foo', 'bar']
    /* two, three etc */
};

for(var i in arrs)
    if (arrs[i].indexOf('foo') != -1)
        return i;

When you have a group of things, store them in an array (if they are ordered) or an object (if they are named). 当您拥有一组事物时,请将它们存储在数组(如果已订购)或对象(如果已命名)中。 If you spot a bunch of variables with almost identical names, you're probably doing something wrong. 如果发现一堆名称几乎相同的变量,则可能是您做错了什么。

 var things = [
    ['a','b','c','d'],
    ['e','f','g','h'],
    ['i','j','k','l']
 ];

Then you can loop over them: 然后,您可以遍历它们:

for (var i = 0; i < things.length; i++) {
    var thing = things[i];
    if (thing.indexOf(id) > -1) { // indexOf returns -1 if not found, and the index (starting from 0) if it is. 
        return i;
    }
}
return null;
var arr1 = ['a', 'b', 'c', 'd'];
var arr2 = ['e', 'f', 'g', 'h'];
var arr3 = ['i', 'j', 'k', 'l'];

function chkElem(elem) {
    var isFound;
    (isFound = arr1).indexOf(elem) > -1 || (isFound = arr2).indexOf(elem) > -1 || (isFound = arr3).indexOf(elem) > -1;
    return isFound;
}

alert(chkElem('f'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM