从数组中获取元素,直到条件匹配

时间:2015-10-14 08:09:22

标签: javascript loops search dictionary

我有一个像这样的数组

$(document).ready(function () {
    $("#myModal").modal("show").on('shown.bs.modal', function () {
        $(".modal").css('display', 'block');
    })
});

我需要获得运营商选择的价值。我有一些像这样的运营商

var arr = [
    "/a_3312368_392921_800MHz", 
    "/a_3312368_392853_900MHz", 
    "/a_3312368_578261_1GHz", 
    "/a_3312368_578189_120GHz", 
    "/a_3312368_578167_140GHz", 
    "/a_3312368_578230_160GHz", 
    "/a_3312368_578192_180GHz", 
    "/a_3312368_397393_183GHz", 
    "/a_3312368_582870_186GHz", 
    "/a_3312368_578174_190GHz", 
    "/a_3312368_578177_2GHz", 
    "/a_3312368_578922_210GHz", 
    "/a_3312368_578479_220GHz", 
    "/a_3312368_578357_226GHz", 
    "/a_3312368_578182_240GHz", 
    "/a_3312368_578919_250GHz", 
    "/a_3312368_578349_253GHz", 
    "/a_3312368_578639_280GHz", 
    "/a_3312368_579178_3GHz"
];

以下是选定的值,例如

`> , < , = , <= , >=`

我当前的运营商

var selection = "/a_3312368_578189_120GHz";

这是我写的一些代码。

var operator = ">";

它给了我正确的结果。但是,当我使用地图进行尝试时,会产生错误var string_array = []; for(var i = 0; i < arr.length; i++){ if(selection == arr[i]){ break; } string_array.push(arr[i]); } console.log(string_array);

Can't have break outside of loop

我如何为此编写一行代码。

Fiddle Link

2 个答案:

答案 0 :(得分:1)

嗯......你不能打破地图。不要使用map。使用someevery

示例 - 部分: - some将在回调返回true时中断

arr.some(function (item) {if (selection == item) {return true;}});    

&#13;
&#13;
var selection = "/a_3312368_578189_120GHz";
var arr = ["/a_3312368_392921_800MHz", "/a_3312368_392853_900MHz", "/a_3312368_578261_1GHz", 
    "/a_3312368_578189_120GHz", "/a_3312368_578167_140GHz", "/a_3312368_578230_160GHz", 
    "/a_3312368_578192_180GHz", "/a_3312368_397393_183GHz", "/a_3312368_582870_186GHz", 
    "/a_3312368_578174_190GHz", "/a_3312368_578177_2GHz", "/a_3312368_578922_210GHz", 
    "/a_3312368_578479_220GHz", "/a_3312368_578357_226GHz", "/a_3312368_578182_240GHz", 
    "/a_3312368_578919_250GHz", "/a_3312368_578349_253GHz", "/a_3312368_578639_280GHz", 
    "/a_3312368_579178_3GHz"
];

string_array =  [];
arr.some(function(item) { if (selection == item){ return true; }else{string_array.push(item);}
});
console.log(string_array);
&#13;
&#13;
&#13;

示例 - 每次: - every将在回调返回时发生false

arr.every(function(item) { if (selection == item) {return false;}});  

&#13;
&#13;
var selection = "/a_3312368_578189_120GHz";
var arr = ["/a_3312368_392921_800MHz", "/a_3312368_392853_900MHz", "/a_3312368_578261_1GHz", 
    "/a_3312368_578189_120GHz", "/a_3312368_578167_140GHz", "/a_3312368_578230_160GHz", 
    "/a_3312368_578192_180GHz", "/a_3312368_397393_183GHz", "/a_3312368_582870_186GHz", 
    "/a_3312368_578174_190GHz", "/a_3312368_578177_2GHz", "/a_3312368_578922_210GHz", 
    "/a_3312368_578479_220GHz", "/a_3312368_578357_226GHz", "/a_3312368_578182_240GHz", 
    "/a_3312368_578919_250GHz", "/a_3312368_578349_253GHz", "/a_3312368_578639_280GHz", 
    "/a_3312368_579178_3GHz"
];

string_array = [];
arr.every(function(item) {if (selection != item){string_array.push(item);} return item.indexOf(selection) == -1;
});

console.log(string_array);
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

使用map时,结果数组的元素数与源数相同 你想要的只是过滤器数组,所以使用filter函数。

此外,如果您的源数组排序器,则运算符只显示与所选元素的索引的关系。

所以你的代码应该是这样的

var selection = "/a_3312368_578189_120GHz";
var indexSelected = arr.indexOf(selection)
string_array = arr.filter(function(el, index){
    return index < indexSelected;
});

var arr = [
    "/a_3312368_392921_800MHz", 
    "/a_3312368_392853_900MHz", 
    "/a_3312368_578261_1GHz", 
    "/a_3312368_578189_120GHz", 
    "/a_3312368_578167_140GHz", 
    "/a_3312368_578230_160GHz", 
    "/a_3312368_578192_180GHz", 
    "/a_3312368_397393_183GHz", 
    "/a_3312368_582870_186GHz", 
    "/a_3312368_578174_190GHz", 
    "/a_3312368_578177_2GHz", 
    "/a_3312368_578922_210GHz", 
    "/a_3312368_578479_220GHz", 
    "/a_3312368_578357_226GHz", 
    "/a_3312368_578182_240GHz", 
    "/a_3312368_578919_250GHz", 
    "/a_3312368_578349_253GHz", 
    "/a_3312368_578639_280GHz", 
    "/a_3312368_579178_3GHz"
];
var selection = "/a_3312368_578189_120GHz";
var indexSelected = arr.indexOf(selection);
var string_array = arr.filter(function(el,index){
  return index < indexSelected;
  });

document.getElementById('r').innerHTML = JSON.stringify(string_array,null, 2);
<pre id="r"></pre>

正如@Toffy在评论中所说 - 在你的情况下,似乎,你需要简单的slice

var indexSelected = arr.indexOf(selection)
string_array = arr.slice(0,indexSelected);

var arr = [
    "/a_3312368_392921_800MHz", 
    "/a_3312368_392853_900MHz", 
    "/a_3312368_578261_1GHz", 
    "/a_3312368_578189_120GHz", 
    "/a_3312368_578167_140GHz", 
    "/a_3312368_578230_160GHz", 
    "/a_3312368_578192_180GHz", 
    "/a_3312368_397393_183GHz", 
    "/a_3312368_582870_186GHz", 
    "/a_3312368_578174_190GHz", 
    "/a_3312368_578177_2GHz", 
    "/a_3312368_578922_210GHz", 
    "/a_3312368_578479_220GHz", 
    "/a_3312368_578357_226GHz", 
    "/a_3312368_578182_240GHz", 
    "/a_3312368_578919_250GHz", 
    "/a_3312368_578349_253GHz", 
    "/a_3312368_578639_280GHz", 
    "/a_3312368_579178_3GHz"
];
var selection = "/a_3312368_578189_120GHz";
var indexSelected = arr.indexOf(selection);
var string_array = arr.slice(0,indexSelected);

document.getElementById('r').innerHTML = JSON.stringify(string_array,null, 2);
<pre id="r"></pre>

源数组未排序的示例

var selection = "/a_3312368_578189_120GHz";
var selectedFreq = getFreq(selection);

function getFreq(s) {
    var parts = s.match(/(\d+)(MHz|GHz)$/);
    return parts[1]* (parts[2] == "GHz" ? 1000: 1);    
}
var string_array = arr.filter(function(el) {
    var elFreq = getFreq(el);
    return selectedFreq > elFreq;
});

var arr = [
  "/a_3312368_392921_800MHz",
  "/a_3312368_392853_900MHz",
  "/a_3312368_578261_1GHz",
  "/a_3312368_578922_210GHz",
  "/a_3312368_578479_220GHz",
  "/a_3312368_578357_226GHz",
  "/a_3312368_578182_240GHz",
  "/a_3312368_578919_250GHz",
  "/a_3312368_578349_253GHz",
  "/a_3312368_578639_280GHz",
  "/a_3312368_579178_3GHz",
  "/a_3312368_578189_120GHz",
  "/a_3312368_578167_140GHz",
  "/a_3312368_578230_160GHz",
  "/a_3312368_578192_180GHz",
  "/a_3312368_397393_183GHz",
  "/a_3312368_582870_186GHz",
  "/a_3312368_578174_190GHz",
  "/a_3312368_578177_2GHz",
];
var selection = "/a_3312368_578189_120GHz";
var selectedFreq = getFreq(selection);

function getFreq(s) {
  var parts = s.match(/(\d+)(MHz|GHz)$/);
  return parts[1]* (parts[2] == "GHz" ? 1000: 1);    
}
var string_array = arr.filter(function(el) {
    var elFreq = getFreq(el);
    return selectedFreq > elFreq;
});

document.getElementById('r').innerHTML = JSON.stringify(string_array, null, 2);
<pre id="r"></pre>