如何在二维数组中找到字符串? [英] How can I find a string in a two dimensional array?

查看:130
本文介绍了如何在二维数组中找到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数组.

I have an array that looks like this.

var array[["a","b"],["c","d"],["e","f"]];         

我希望能够在数组中搜索字符串"d" 并返回相应的值"c" .

I want to be able to search through the array for the string "d" and return the corresponding value "c".

推荐答案

尝试:

function find_str(array){
  for(var i in array){
    if(array[i][1] == 'd'){
      return array[i][0];
    }
  }
}

function find_str(array){
  for(var i=0;i<array.length;i++){
    if(array[i][1] == 'd'){
      return array[i][0];
    }
  }
}

这篇关于如何在二维数组中找到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆