javascript - JS query whether the first 8 or first 6 digits of a set of 10 digits match
phpcn_u1582
phpcn_u1582 2017-05-19 10:45:33
0
3
543

I want to implement a function, that is, when the user enters 10 digits in input and clicks the query button, it will match the pre-input value in the json data. , and feed back different query results based on different information.

However, the input-ahead value of json is only 8 or 6 digits (corresponding to the first 8 or 6 digits of the 10-digit number, the matching priority is 8 digits first and 6 digits later). Please tell me how to make this query judgment? Thanks!

jsonFormat:

{"id":"123456","info":"这是普通用户"},
{"id":"87654321","info":"这是高级用户"}
{"id":"876543","info":"这是尊享用户"}

For example, the user input is 1234567890, and the pre-input value in the json data is 123456, Then match the information of this input-ahead value. If the user input is 0987654321, the input-ahead value in the json data is 098765# When both ## and 09876543 are used, the information of the pre-input value of 09876543 will be matched.

PS: I have fallen into a dead end of fuzzy query, and I can’t get around it. . .

phpcn_u1582
phpcn_u1582

reply all(3)
为情所困
var jsons = [
            {"id":"123456","info":"这是普通用户"},
            {"id":"876543","info":"这是尊享用户"},
            {"id":"87654321","info":"这是高级用户"},
            {"id":"87654311","info":"这是尊享用户"},
         ]    
    function f(jsons,num) {
        var num8 = num.substr(0,8);
        var num6 = num.substr(0,6);
        var result6 = '';
        var result8 = '';
    
          for(var i = 0,len = jsons.length; i < len;i++) {
              var id = jsons[i].id;
              if(id == num8) {
                  result8 = jsons[i];
                  break;
              }
    
              if(id == num6) {
                  result6 = jsons[i];
              }
           }
    
          return result8 ? result8 : result6; 
    }
刘奇
var json = [{'id': '123456','info': '这是普通用户'},
  {'id': '87654321','info': '这是高级用户'},
  { 'id': '876543', 'info': '这是尊享用户' }]

var value = '8765432151'
function slice (value, wei) {
  var temp = [].slice.call(value, 0, wei)
  temp = temp.join('')
  return temp
}

function match (userid, data) {
  var id6 = slice(userid, 6)
  var id8 = slice(userid, 8)
  for (var i = 0; i < json.length; i++) {
      // console.log(json[i])    
      console.log(slice(json[i].id, 8),id8)
    if (slice(json[i].id, 8) == id8||slice(json[i].id, 6) == id6) {
      var info = json[i].info
      info = slice(json[i].id, 6) === id6 ? json[i].info : info
      return info;
    }
  }
  
}

console.log(match(value, json))

It’s very cheating. You need to traverse all the data, so let the background do this kind of thing.

过去多啦不再A梦
先处理一下json
var jsonObj={};
json.forEach((x)=>{jsonObj[x.id]=x});
function checkFn(val){
     var val8=val.slice(0,8);
        var val6=val.slice(0,6)
        if(jsonObj[val8]){
           //匹配到8位
        }else if(jsonObj[val6]){
            //匹配到6位)
        }else{
            //无匹配
        }
}
   
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!