可以告訴我如何使用Underscore.js找出屬性的第一個匹配項(不需要完全匹配)?要找出屬性的第一個匹配項(不需要完全匹配)
我已經試過this.But它在我的名單,其中有townName
像Rocky Point
上只返回精確匹配的property.Let的發言權。但我需要選擇Rocky
also.So我該怎麼辦呢?
var schoolDistrict = _.find(vm.schoolDistricts, function (d) { return d.townName
== result.data[0].PropertyData.School; });
例如,
vm.schoolDistricts = [townName:'Rocky Point',...]
result.data[0].PropertyData.School = 'Rocky'
嘗試'd.townName.indexOf(result.data [0] .Prop ertyData.School)!== -1'。這將檢查城鎮名稱的字符串是否包含學校名稱的字符串,而不是它們需要精確。如果你還需要在'Rocky Point'中找到'rocky',你可以在這兩個字符串中添加'toLowerCase()'。 – Shilly
@Shilly您的解決方案也無法正常工作。這是代碼片段.'var schoolDistrict = _.find(vm.schoolDistricts,function(s){return s.townName.indexOf(result.data [0] .PropertyData.School )!== -1;});' – Sampath
@Shilly你是對的,我必須使用'toLowerCase()'。然後它才能工作。你想把它當作答案嗎?謝謝。 – Sampath