<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          每天一點(diǎn)前端知識 - 數(shù)據(jù)過濾

          共 2610字,需瀏覽 6分鐘

           ·

          2023-08-24 04:49

          實(shí)現(xiàn)

              const filter = (list, keyword, fields = [], exact = false, str2Dom = null) => {
          if (!isValidArr(list)) {
          return [];
          }
          if (!keyword) {
          return list;
          }
          fields = typeof fields === 'string' ? fields.split(',') : fields;
          return list.filter(v => {
          fields = fields.length > 0 ? fields : Object.keys(v);
          const matched = fields.filter(field => {
          const fieldValue = v[field];
          if (fieldValue == null) {
          return false;
          }
          if (exact) {
          return fieldValue === keyword;
          }
          const reg = new RegExp(keyword, 'gi');
          const match = fieldValue.toString().match(reg);
          // 高亮
          if (match && str2Dom) {
          v[field] = str2Dom(fieldValue.toString().replace(reg, `<span style="background-color:yellow">${match[0]}</span>`), {display: 'inline-block'});
          }
          return match;
          });
          return matched.length;
          });
          };


          const getMatched = fn =>
          (arr, childKey = 'children') => {
          if (!Array.isArray(arr)) {
          return arr;
          }
          const list = clone(arr);
          const traver = data => {
          const matchedData = [];
          data.map(v => {
          if (isValidArr(v[childKey])) {
          const matchChildren = traver(v[childKey]) || [];
          v[childKey] = matchChildren;
          if (matchChildren.length > 0) {
          matchedData.push(v);
          }
          }
          });
          return fn(data, matchedData);
          };
          return traver(list);
          };
          const filterList = (data, keyword, fields = 'name', exact = false, idKey = 'id', childKey = 'children', str2Dom) => getMatched((list, matchedItem) => unique([...filter(list, keyword, fields, exact, str2Dom), ...matchedItem], idKey))(data, childKey);

          使用

              filterList(data, keyword, fields = 'name', exact = false, idKey = 'id', childKey = 'children', null);

          - data:列表數(shù)據(jù)
          - keyword:搜索值
          - fields:搜索字段,字符串或數(shù)組。根據(jù)屬性過濾,默認(rèn) `name`,設(shè)為 `null` 則全局所有屬性搜索
          - exact:是否為精確匹配,默認(rèn)是模糊搜索
          - idKey:節(jié)點(diǎn)唯一標(biāo)識符
          - childKey:子節(jié)點(diǎn)屬性值
          - str2Dom:高亮關(guān)鍵詞

          示例

              const data = [
          {
          path: "/a",
          name: "a",
          parentId: "",
          children: [
          { path: "/a/1", name: "a1", parentId: "/a", children: [] },
          { path: "/a/3", name: "a3", parentId: "/a", children: [] },
          { path: "/a/2", name: "a2", parentId: "/a", children: [] },
          ],
          },
          {
          path: "/c",
          name: "c",
          parentId: "",
          children: [
          { path: "/c/1", name: "c1", parentId: "/c", children: [] },
          { path: "/c/2", name: "c2", parentId: "/c", children: [] },
          ],
          },
          {
          path: "/b",
          name: "b",
          parentId: "",
          children: [
          {
          path: "/b/1",
          name: "b1",
          parentId: "/b",
          children: [
          { path: "/b/1/1", name: "b11", parentId: "/b/1", children: [] },
          ],
          },
          { path: "/b/2", name: "b2", parentId: "/b", children: [] },
          ],
          },
          ];

          filterList(data, '/1', 'path', false, 'path');

          be7a9c0cea4f29344a2bcd2f5150b054.webp

          1bd97af42ee0eb74dc9b00df234f7712.webp

          高亮關(guān)鍵詞:

          71dc92a1f55d3cf0a1992b5955fa248d.webp

          演示地址:https://ihuxy.com/play?utils=filterList


          瀏覽 35
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲综合 p| 国产午夜福利视频 | 免费国产A片 | 在线中文字幕av 中文字幕久久精品 | 日本亚洲无免费码在线 |