在 Json 中搜索过滤器

Search In Json for filters

我有一个 JSON 像这样的对象

 xyz = {
  "101": {
    "categoryName": "ectronics",
    "categorslug": "electronics",
    "catId": "101",
    "name": [
      {
        "childCategoryName": "Cameras",
        "childCategorySlug": "cameras",
        "childCategoryId": "102",
        "childCategoryParentId": "109"
      },
      {
        "childCategoryName": "Accessories",
        "childCategorySlug": "cameras-and-accessories",
        "childCategoryId": "102",
        "childCategoryParentId": "111"
      },
      {
        "childCategoryName": "ras & Accessories",
        "childCategorySlug": "cameras-and-accessories",
        "childCategoryId": "102",
        "childCategoryParentId": "112"
      }
    ]
  },
  "109": {
    "categoryName": "shion",
    "categorslug": "fashion",
    "catId": "109",
    "name": [
      {
        "childCategoryName": "Fashion Accessories",
        "childCategorySlug": "fashion-accessories",
        "childCategoryId": "110",
        "childCategoryParentId": "109"
      }
    ]
  },
  "118": {
    "categoryName": "Baby & Kids",
    "categorslug": "baby-and-kids",
    "catId": "118",
    "name": [
      {
        "childCategoryName": "Baby Footwear",
        "childCategorySlug": "baby-footwear",
        "childCategoryId": "119",
        "childCategoryParentId": "118"
      }
    ]
  }
}

现在我有一个基于搜索词的类别过滤器,我必须减少对象,我正在这样做,但问题是我无法从匹配的对象中减少它。

我尝试的解决方案是,

topIds = (event) => {
    let rs = [];
    let inn = '';
    let cat = xyz;
    for (let i in cat){
        for( let j in cat[i].name ){
            inn = cat[i].name[j].childCategoryName
            if ( inn.toLowerCase().search(event.target.value.toLowerCase()) !== -1 ){
                rs.push(cat[i].name[j].childCategoryParentId)
            }
        }
    }
    console.log('Filtered Ids', rs);
    this.elm(cat, rs)
  }

  elm = (cat, rs) => {
    let filtered =  rs.map(function (k){
        return cat[k]
    })
    console.log('Filtered va;ues', filtered);
    return filtered;
  }

它工作正常但没有匹配它的 returning 完整对象任何建议我在这里做错了什么。

所以,从技术上讲,如果用户说搜索 "Cameras"

应该return,像这样,

{
      "101": {
        "categoryName": "ectronics",
        "categorslug": "electronics",
        "catId": "101",
        "name": [
          {
            "childCategoryName": "Cameras",
            "childCategorySlug": "cameras",
            "childCategoryId": "102",
            "childCategoryParentId": "109"
          }
       ]
      }
}

先谢谢大家了。

据我了解.. 注意,您必须复制找到的 object,对应的 child

也是如此

const xyz = 
  { 101: 
    { categoryName: 'ectronics'
    , categorslug : 'electronics'
    , catId       : '101'
    , name: 
      [ { childCategoryName    : 'Cameras'
        , childCategorySlug    : 'cameras'
        , childCategoryId      : '102'
        , childCategoryParentId: '109'
        } 
      , { childCategoryName    : 'Accessories'
        , childCategorySlug    : 'cameras-and-accessories'
        , childCategoryId      : '102'
        , childCategoryParentId: '111'
        } 
      , { childCategoryName    : 'ras & Accessories'
        , childCategorySlug    : 'cameras-and-accessories'
        , childCategoryId      : '102'
        , childCategoryParentId: '112'
    } ] } 
  , 109: 
    { categoryName: 'shion'
    , categorslug : 'fashion'
    , catId       : '109'
    , name: 
      [ { childCategoryName    : 'Fashion Accessories'
        , childCategorySlug    : 'fashion-accessories'
        , childCategoryId      : '110'
        , childCategoryParentId: '109'
    } ] } 
  , 118: 
    { categoryName: 'Baby & Kids'
    , categorslug : 'baby-and-kids'
    , catId       : '118'
    , name: 
      [ { childCategoryName    : 'Baby Footwear'
        , childCategorySlug    : 'baby-footwear'
        , childCategoryId      : '119'
        , childCategoryParentId: '118'
  } ] } } 

function getPath2childCategoryName(obj,val)
  {
  let child = null
    , resp  = {}
    ;
  for(let key in obj)
    {
    child = obj[key].name.find(n=>n.childCategoryName===val)
    if (child)
      {
      resp[key]      = {...obj[key]} 
      resp[key].name = [ {...child} ]
      break
      }
    }
  return resp
  }

let bob = getPath2childCategoryName(xyz,'Cameras')

console.log( JSON.stringify(bob,0,2) )
.as-console-wrapper { max-height: 100% !important; top: 0; }