如何搜索数组并返回匹配值

时间:2017-11-25 21:54:12

标签: javascript jquery arrays scope return

我有一些类名的数组,该数组中的所有值都以数字结尾。我想要一个函数,当我给它一个数字时,它搜索该数组,并检查每个值的最后3位数,找到匹配的数字并返回该匹配值。 所以我要说,我给它赋值200,然后搜索数组并返回wi-owm-200

我设法成功,但它没有返回它。它确实找到了值,但是当它在函数之外时,它返回' undefined'。

这是我的代码:Fiddle



var owmIcon = ["wi-owm-200", "wi-owm-201", "wi-owm-202", "wi-owm-210", "wi-owm-211", "wi-owm-212", "wi-owm-221", "wi-owm-230", "wi-owm-231", "wi-owm-232", "wi-owm-300", "wi-owm-301", "wi-owm-302", "wi-owm-310", "wi-owm-311", "wi-owm-312", "wi-owm-313", "wi-owm-314", "wi-owm-321", "wi-owm-500", "wi-owm-501", "wi-owm-502", "wi-owm-503", "wi-owm-504", "wi-owm-511", "wi-owm-520", "wi-owm-521", "wi-owm-522", "wi-owm-531", "wi-owm-600", "wi-owm-601", "wi-owm-602", "wi-owm-611", "wi-owm-612", "wi-owm-615", "wi-owm-616", "wi-owm-620", "wi-owm-621", "wi-owm-622", "wi-owm-701", "wi-owm-711", "wi-owm-721", "wi-owm-731", "wi-owm-741", "wi-owm-761", "wi-owm-762", "wi-owm-771", "wi-owm-781", "wi-owm-800", "wi-owm-801", "wi-owm-802", "wi-owm-803", "wi-owm-804", "wi-owm-900", "wi-owm-901", "wi-owm-902", "wi-owm-903", "wi-owm-904", "wi-owm-905", "wi-owm-906", "wi-owm-957"];


var res = findOWMIcon("200");
console.log(res);


function findOWMIcon(num) {
  $.each(owmIcon, function(key, value) {
    var classNum = value.substr(value.length - 3);
    if (parseInt(num, 10) === parseInt(classNum, 10)) {
      console.log(value);
      return value;
    }
  });
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

知道怎么解决吗?所以我可以在函数外得到找到的值?

2 个答案:

答案 0 :(得分:2)

这就足够了:

owmIcon.filter(e => e.slice(-3) == "200");

// Get all elements that ends in "01"
items = get_all_items().filter(e => e.slice(-2) == "01");
console.log(items);

function get_all_items(){
  return ["wi-owm-200", "wi-owm-201", "wi-owm-202", "wi-owm-210", "wi-owm-211", "wi-owm-212", "wi-owm-221", "wi-owm-230", "wi-owm-231", "wi-owm-232", "wi-owm-300", "wi-owm-301", "wi-owm-302", "wi-owm-310", "wi-owm-311", "wi-owm-312", "wi-owm-313", "wi-owm-314", "wi-owm-321", "wi-owm-500", "wi-owm-501", "wi-owm-502", "wi-owm-503", "wi-owm-504", "wi-owm-511", "wi-owm-520", "wi-owm-521", "wi-owm-522", "wi-owm-531", "wi-owm-600", "wi-owm-601", "wi-owm-602", "wi-owm-611", "wi-owm-612", "wi-owm-615", "wi-owm-616", "wi-owm-620", "wi-owm-621", "wi-owm-622", "wi-owm-701", "wi-owm-711", "wi-owm-721", "wi-owm-731", "wi-owm-741", "wi-owm-761", "wi-owm-762", "wi-owm-771", "wi-owm-781", "wi-owm-800", "wi-owm-801", "wi-owm-802", "wi-owm-803", "wi-owm-804", "wi-owm-900", "wi-owm-901", "wi-owm-902", "wi-owm-903", "wi-owm-904", "wi-owm-905", "wi-owm-906", "wi-owm-957"];
}

如果您关心向后兼容性(ES5及以前版本),请使用function(e){ return e.slice(-3) == "200"; }代替e => ...

答案 1 :(得分:1)

let data = ["wi-owm-200", "wi-owm-201", "wi-owm-202", "wi-owm-210", "wi-owm-211", "wi-owm-212", "wi-owm-221", "wi-owm-230", "wi-owm-231", "wi-owm-232", "wi-owm-300", "wi-owm-301", "wi-owm-302", "wi-owm-310", "wi-owm-311", "wi-owm-312", "wi-owm-313", "wi-owm-314", "wi-owm-321", "wi-owm-500", "wi-owm-501", "wi-owm-502", "wi-owm-503", "wi-owm-504", "wi-owm-511", "wi-owm-520", "wi-owm-521", "wi-owm-522", "wi-owm-531", "wi-owm-600", "wi-owm-601", "wi-owm-602", "wi-owm-611", "wi-owm-612", "wi-owm-615", "wi-owm-616", "wi-owm-620", "wi-owm-621", "wi-owm-622", "wi-owm-701", "wi-owm-711", "wi-owm-721", "wi-owm-731", "wi-owm-741", "wi-owm-761", "wi-owm-762", "wi-owm-771", "wi-owm-781", "wi-owm-800", "wi-owm-801", "wi-owm-802", "wi-owm-803", "wi-owm-804", "wi-owm-900", "wi-owm-901", "wi-owm-902", "wi-owm-903", "wi-owm-904", "wi-owm-905", "wi-owm-906", "wi-owm-957"];

// Suppose you want to get all elements that end with 200, so...

let filteredData = data.filter(item => item.slice(-3) === "200");

console.log(filteredData);