使用 for 循环的 array.some() 方法问题

var phaseOne = [

  {

    streetNumberLow: "1",

    streetNumberHigh: "436",

    streetName: "barnhart",

    streetCode: "rd",

    city: "waynesburo",

    state: "va",

    zipCode: "22980",

  }

];


var phaseOneExt = [

  {

    streetNumberLow: "580",

    streetNumberHigh: "1002",

    streetName: "battlefield",

    streetCode: "rd",

    city: "fort defiance",

    state: "va",

    zipCode: "24437",

  }

];


var phaseTwo = [

  {

    streetNumberLow: "1",

    streetNumberHigh: "727",

    streetName: "bailey",

    streetCode: "rd",

    city: "fort defiance",

    state: "va",

    zipCode: "24437",

  }

];


var phaseTwoCares = [

  {

    streetNumberLow: "728",

    streetNumberHigh: "996",

    streetName: "bailey",

    streetCode: "rd",

    city: "fort defiance",

    state: "va",

    zipCode: "24437",

  }

];


function parseAddressString(str) {

  var matches = str.match(

    /(?<streetNumber>\d+)\s+(?<streetName>[\w\s-]+)\s+(?<streetCode>\w+),?\s+(?<city>[\w\s-]+),?\s+(?<state>\w+),?\s+(?<zipCode>\d+)/

  );

  if (matches) {

    return matches.groups;

  }

  return false;

}


function doesStringMatch(a, b) {

  return a.toLowerCase().includes(b.toLowerCase());

}

function doesAddressMatchPhase(address, phase) {

  return (

    address &&

    +address.streetNumber >= +phase.streetNumberLow &&

    +address.streetNumber <= +phase.streetNumberHigh &&

    doesStringMatch(address.streetName, phase.streetName) &&

    doesStringMatch(address.streetCode, phase.streetCode) &&

    doesStringMatch(address.city, phase.city) &&

    doesStringMatch(address.state, phase.state) &&

    doesStringMatch(address.zipCode, phase.zipCode)

  );

}

我目前正在构建一个 JavaScript 工具,它将地址作为输入,并将其与服务区域内的临时地址数据库进行比较。我使用 array.some() 方法循环访问服务区域的每个相数组中的每个对象属性。如果前一个阶段返回 false,我还需要它来查看每个阶段。因为我使用 for 循环来循环遍历每个相位数组,所以 .some 方法中所需的返回会导致我的函数在第一个循环中退出。问题出在 isEligible(str) 函数内。附加的代码片段中是损坏的代码。如果我将 return 放在函数的最后两行前面,我可以让它工作,但如果在第一阶段内找不到地址,它不会让循环继续。



慕莱坞森
浏览 89回答 1
1回答

至尊宝的传说

这eligible = isEligible(str);是一个逻辑错误。eligible永远是undefined因为isEligible()没有返回值。(我不得不这么说,尽管你的评论表明你知道这一点)。要解决此问题,请返回结果doesAddressMatchPhase()并将返回结果存储.some()在变量中。然后在.some()调用之后立即测试该变量是否为真,如果是,则中断循环并立即返回该变量。否则,等到函数结束时返回默认 false。这是工作解决方案:var phaseOne = [&nbsp; {&nbsp; &nbsp; streetNumberLow: "1",&nbsp; &nbsp; streetNumberHigh: "436",&nbsp; &nbsp; streetName: "barnhart",&nbsp; &nbsp; streetCode: "rd",&nbsp; &nbsp; city: "waynesburo",&nbsp; &nbsp; state: "va",&nbsp; &nbsp; zipCode: "22980",&nbsp; }];var phaseOneExt = [&nbsp; {&nbsp; &nbsp; streetNumberLow: "580",&nbsp; &nbsp; streetNumberHigh: "1002",&nbsp; &nbsp; streetName: "battlefield",&nbsp; &nbsp; streetCode: "rd",&nbsp; &nbsp; city: "fort defiance",&nbsp; &nbsp; state: "va",&nbsp; &nbsp; zipCode: "24437",&nbsp; }];var phaseTwo = [&nbsp; {&nbsp; &nbsp; streetNumberLow: "1",&nbsp; &nbsp; streetNumberHigh: "727",&nbsp; &nbsp; streetName: "bailey",&nbsp; &nbsp; streetCode: "rd",&nbsp; &nbsp; city: "fort defiance",&nbsp; &nbsp; state: "va",&nbsp; &nbsp; zipCode: "24437",&nbsp; }];var phaseTwoCares = [&nbsp; {&nbsp; &nbsp; streetNumberLow: "728",&nbsp; &nbsp; streetNumberHigh: "996",&nbsp; &nbsp; streetName: "bailey",&nbsp; &nbsp; streetCode: "rd",&nbsp; &nbsp; city: "fort defiance",&nbsp; &nbsp; state: "va",&nbsp; &nbsp; zipCode: "24437",&nbsp; }];function parseAddressString(str) {&nbsp; var matches = str.match(&nbsp; &nbsp; /(?<streetNumber>\d+)\s+(?<streetName>[\w\s-]+)\s+(?<streetCode>\w+),?\s+(?<city>[\w\s-]+),?\s+(?<state>\w+),?\s+(?<zipCode>\d+)/&nbsp; );&nbsp; if (matches) {&nbsp; &nbsp; return matches.groups;&nbsp; }&nbsp; return false;}function doesStringMatch(a, b) {&nbsp; return a.toLowerCase().includes(b.toLowerCase());}function doesAddressMatchPhase(address, phase) {&nbsp; return (&nbsp; &nbsp; address &&&nbsp; &nbsp; +address.streetNumber >= +phase.streetNumberLow &&&nbsp; &nbsp; +address.streetNumber <= +phase.streetNumberHigh &&&nbsp; &nbsp; doesStringMatch(address.streetName, phase.streetName) &&&nbsp; &nbsp; doesStringMatch(address.streetCode, phase.streetCode) &&&nbsp; &nbsp; doesStringMatch(address.city, phase.city) &&&nbsp; &nbsp; doesStringMatch(address.state, phase.state) &&&nbsp; &nbsp; doesStringMatch(address.zipCode, phase.zipCode)&nbsp; );}function isEligible(str) {&nbsp; var address = parseAddressString(str);&nbsp; var phases = [phaseOne, phaseOneExt, phaseTwo, phaseTwoCares];&nbsp; var x;&nbsp; let found = false;&nbsp; for (x = 0; x < phases.length; x++) {&nbsp; &nbsp; found = phases[x].some(function (phase) {&nbsp; &nbsp; &nbsp; return doesAddressMatchPhase(address, phase);&nbsp; &nbsp; });&nbsp; &nbsp; if(found) {&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; }&nbsp; return found;}function addressValidation() {&nbsp; var str = document.getElementById("address").value,&nbsp; &nbsp; eligible = isEligible(str);&nbsp; document.getElementById("demo").innerHTML = eligible ? "true" : "false";}@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Poppins:wght@400;700&display=swap');/* Global */* {&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 0;&nbsp; &nbsp; font-family: 'Bebas Neue', cursive;}#address-checker {&nbsp; &nbsp; height:25vh;&nbsp; &nbsp; width: 60%;&nbsp; &nbsp; margin: auto;&nbsp; &nbsp; margin-top: -3vh;&nbsp; &nbsp; padding: 3vw;&nbsp; &nbsp; background-color: #fff;&nbsp; &nbsp; position: relative;&nbsp; &nbsp; z-index: 1;&nbsp; &nbsp; box-shadow: 0 .5rem 1rem rgba(0, 0, 0, 0.5);}.checker-container {&nbsp; &nbsp; height: 25vh;}.address-header {&nbsp; &nbsp; padding: 3vh 0 2vh 0;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; color: #2277AE;&nbsp; &nbsp; letter-spacing: 1px;&nbsp; &nbsp; font-size: clamp( 1.5rem, 3.5vw, 3.5rem);}.address-checker-input {&nbsp; &nbsp; padding: 1vh 0;}.address-box {&nbsp; &nbsp; margin: 1.5vh 0 0 10vw ;&nbsp; &nbsp; width: 60%;&nbsp; &nbsp; padding: .75rem 0 .75rem .5rem;&nbsp; &nbsp; border-radius: 10px;&nbsp; &nbsp; border: #2277AE 2px solid;&nbsp; &nbsp; color: #2277AE;}.address-box:hover {&nbsp; &nbsp; border-color: #FEA00B;&nbsp; &nbsp; transition: .2s;}.go-btn {&nbsp; background-color: #2277AE;&nbsp; padding: .75rem;&nbsp; border-radius: 10px;&nbsp; border: #2277AE 2px solid;&nbsp; color: #fff;}.go-btn:hover {&nbsp; &nbsp; border-color: #FEA00B;&nbsp; &nbsp; background-color: #FEA00B;&nbsp; &nbsp; transition: .2s;}<html><head>&nbsp; &nbsp; <link rel="stylesheet" href="style.css"></head><body>&nbsp;&nbsp;<section id="address-checker">&nbsp; &nbsp; &nbsp; <div class="checker-container">&nbsp; &nbsp; &nbsp; &nbsp; <h1 class="address-header">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Is fiber to the home available for you?&nbsp; &nbsp; &nbsp; &nbsp; </h1>&nbsp; &nbsp; &nbsp; &nbsp; <div class="address-checker-input">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id="address"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name="searchaddress"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder="Street Address, City, State, Zip Code"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="address-box"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value="200 barnhart rd waynesburo, va 22980"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="go-btn" onclick="addressValidation()">Go</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <p id="demo"></p>&nbsp; &nbsp; &nbsp; </div></section></body><script src="main.js"></script></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript