数组循环匹配的问题

小太阳大晴天
  • 69

需求:通过城市选择器,获取城市code,然后code里的每项去匹配lvOne和lvTwo。
在lvOne里匹配到只要有一项满足toLevelCity就返回lvOne
在lvTwo里匹配到只要有一项满足toLevelCity就返回lvTwo
否者toLevelCity返回other

let code = ["110000", "110101"]
let lvOne = [110100, 310100, 440100, 440300, 330100, 320100, 320500, 330200, 510100, 420100, 500100, 350200]
let lvTwo = [120000, 370100, 131000, 370200, 210200, 430200, 230100, 210100, 610100, 220100, 430100, 350100, 410100, 130100, 440600, 441900, 320200, 370600, 140100, 340100, 360100, 450100, 530100, 330300, 370300, 130200, 650100, 520100, 460100, 620100, 640100, 630100, 150100, 350500, 150200, 320600, 230600, 320300, 370700, 320400, 330600, 370800, 320900, 371300, 410300, 370500, 321000, 331000, 330400, 130900, 321200, 321100, 330700, 130600, 220200, 210300, 370900, 420500, 442000, 441300, 371000, 371400, 371500, 430700, 350600, 371600, 440900, 320800, 440700, 340200, 440800, 450200, 440400, 510700, 520300, 371700]
let arr = code.split(',');
let {LevelOneCities,LevelTwoCities} = LvCities;
let toLevelCity = null;

我尝试了用.filter().some()但是都不行。
太菜了我。。>_<

回复
阅读 516
1 个回答
✓ 已被采纳

image.png

var toLevelCity = [];
if(code.some(v=>lvOne.includes(+v))) toLevelCity.push('lvOne');
if(code.some(v=>lvTwo.includes(+v))) toLevelCity.push('lvOne');
if(toLevelCity.length == 0) toLevelCity.push('other');
你知道吗?

宣传栏