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!
json
Format:
{"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. . .
为情所困2017-05-19 10:47:33
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;
}
怪我咯2017-05-19 10:47:33
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梦2017-05-19 10:47:33
先处理一下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{
//无匹配
}
}