Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JS中搜索多维数组中的键值?_Javascript_Arrays - Fatal编程技术网

Javascript 如何在JS中搜索多维数组中的键值?

Javascript 如何在JS中搜索多维数组中的键值?,javascript,arrays,Javascript,Arrays,我在一个处理数据仓库的应用程序的前端工作,所以我有很多带有字段名和数据类型的数组 我有一个如下所示的数组: ["CODART"]["S"], ["DESCART"]["A"], ["EXST"]["S"] selex2.push(Element, dataType); 我想按键搜索,得到另一个值,如 科达特和我有两个孩子 提前谢谢 无法使用jquery,只能使用纯JS 我创建的数组如下所示: ["CODART"]["S"], ["DESCART"]["A"], ["EXST"]["S"]

我在一个处理数据仓库的应用程序的前端工作,所以我有很多带有字段名和数据类型的数组

我有一个如下所示的数组:

["CODART"]["S"],
["DESCART"]["A"],
["EXST"]["S"]
selex2.push(Element, dataType);
我想按键搜索,得到另一个值,如 科达特和我有两个孩子

提前谢谢

无法使用jquery,只能使用纯JS

我创建的数组如下所示:

["CODART"]["S"],
["DESCART"]["A"],
["EXST"]["S"]
selex2.push(Element, dataType);

现在,我想搜索第一个值以获得第二个值。

我猜多维数组的意思是如下所示

[
    ["KEY", "VALUE"],
    ["KEY2", "VALUE2"],
]
const value = potential[1];
if (potential) {
  // You're save, potential is defined and you can extract the value.
} else {
  // Watch out! Potential is undefined and trying to extract the value
  // will lead to an error.
}
首先,这不是JS中使用的最佳数据结构类型。但是,要根据键或键2查找值或值2,需要以下代码

// Find the array where 'KEY' is our first value.
const potential = array.find(([key]) => key === 'KEY');
在变量势中,现在有[KEY,VALUE]。如果绝对保存,并且搜索的密钥有一个值,则可以使用以下命令提取该值

[
    ["KEY", "VALUE"],
    ["KEY2", "VALUE2"],
]
const value = potential[1];
if (potential) {
  // You're save, potential is defined and you can extract the value.
} else {
  // Watch out! Potential is undefined and trying to extract the value
  // will lead to an error.
}
如果您不确定,您应该检查是否定义了电势。您可以使用以下方法来完成此操作

[
    ["KEY", "VALUE"],
    ["KEY2", "VALUE2"],
]
const value = potential[1];
if (potential) {
  // You're save, potential is defined and you can extract the value.
} else {
  // Watch out! Potential is undefined and trying to extract the value
  // will lead to an error.
}

希望我能帮忙

这不是一个有效的数组?请共享一个语法上可行的代码示例。编辑了我的问题,如果您添加填充数组的代码,我们可以继续。元素和数据类型只是几个带字符串值的变量。谢谢。谢谢罗德里戈,这对我有用。@rogerluces很棒,很高兴我能帮上忙。你能把我的回答标记为接受,这样其他有类似问题的人也能受益吗?提前谢谢!