JS_js一维数组对象模糊查询

var type = ["name", "id"];
    var dataList = [
      { name: "蓝有",number: "1" },
      { name: "蓝天", number: "2" },
      { name: "李天一", number: "3" },
      { name: "懒", number: "4" },
    ]
    var value = "天";

/*一维数组对象模糊搜索
  dataList 为一维数组数据结构
  value 为input框的输入值
  type 为指定想要搜索的字段名,array格式 ["name", "number"];
 */
function filterOne(dataList, value, type) {
  var s = dataList.filter(function(item, index, arr) {
    console.log(item)
    for (let j = 0; j < type.length; j++) {
      if (item[type[j]] != undefined || item[type[j]] != null) {
        if (item[type[j]].indexOf(value) >= 0) {
          return item;
        }
      }
    }
  });
  return s;
}

你可能感兴趣的:(#,JS)